Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
raporpe committed Nov 26, 2024
1 parent 98f3243 commit 7d05903
Show file tree
Hide file tree
Showing 4 changed files with 494 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,39 +1,49 @@
namespace Microsoft.ComponentDetection.Contracts.TypedComponent;

using System.Collections.Generic;
using Newtonsoft.Json;

/// <summary>
/// Represents a SwiftPM component.
/// </summary>
public class SwiftPMResolvedFile
{
[JsonProperty("pins")]
public IList<SwiftPMDependency> Pins { get; set; }

[JsonProperty("version")]
public int Version { get; set; }

public class SwiftPMDependency
{
// The name of the package
[JsonProperty("identity")]
public string Identity { get; set; }

// How the package is imported. Example: "remoteSourceControl"
[JsonProperty("kind")]
public string Kind { get; set; }

// The unique path to the repository where the package is located. Example: Git repo URL.
[JsonProperty("location")]
public string Location { get; set; }

// Data about the package version and commit hash.
[JsonProperty("state")]
public SwiftPMState State { get; set; }

public class SwiftPMState
{
// The commit hash of the package.
[JsonProperty("revision")]
public string Revision { get; set; }

// The version of the package. Might be missing.
[JsonProperty("version")]
public string Version { get; set; }

// The branch of the package. Might be missing.
[JsonProperty("branch")]
public string Branch { get; set; }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,14 @@ protected override Task OnFileFoundAsync(
{
this.Logger.LogInformation("SwiftPMComponentDetector: Found Package.resolved file: {Location}", processRequest.ComponentStream.Location);

this.ProcessPackageResolvedFile(processRequest.SingleFileComponentRecorder, processRequest.ComponentStream);
try
{
this.ProcessPackageResolvedFile(processRequest.SingleFileComponentRecorder, processRequest.ComponentStream);
}
catch (Exception exception)
{
this.Logger.LogError(exception, "SwiftPMComponentDetector: Error processing Package.resolved file: {Location}", processRequest.ComponentStream.Location);
}

return Task.CompletedTask;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
namespace Microsoft.ComponentDetection.Detectors.Tests.SwiftPM;

using System;
using System.Collections.Generic;
using FluentAssertions;
using Microsoft.ComponentDetection.Contracts.TypedComponent;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PackageUrl;

[TestClass]
public class SwiftPMComponentTests
{
[TestMethod]
public void Constructor_ShouldInitializeProperties()
{
var name = "alamofire";
var version = "5.9.1";
var packageUrl = "https://github.com/Alamofire/Alamofire";
var hash = "f455c2975872ccd2d9c81594c658af65716e9b9a";

var component = new SwiftPMComponent(name, version, packageUrl, hash);

component.Name.Should().Be(name);
component.Version.Should().Be(version);
component.Type.Should().Be(ComponentType.SwiftPM);
component.Id.Should().Be($"{name} {version} - {component.Type}");
}

[TestMethod]
public void Constructor_ShouldThrowException_WhenNameIsNull()
{
Action action = () => new SwiftPMComponent(null, "5.9.1", "https://github.com/Alamofire/Alamofire", "f455c2975872ccd2d9c81594c658af65716e9b9a");
action.Should().Throw<ArgumentException>().WithMessage("*name*");
}

[TestMethod]
public void Constructor_ShouldThrowException_WhenVersionIsNull()
{
Action action = () => new SwiftPMComponent("alamofire", null, "https://github.com/Alamofire/Alamofire", "f455c2975872ccd2d9c81594c658af65716e9b9a");
action.Should().Throw<ArgumentException>().WithMessage("*version*");
}

[TestMethod]
public void Constructor_ShouldThrowException_WhenPackageUrlIsNull()
{
Action action = () => new SwiftPMComponent("alamofire", "5.9.1", null, "f455c2975872ccd2d9c81594c658af65716e9b9a");
action.Should().Throw<ArgumentException>().WithMessage("*packageUrl*");
}

[TestMethod]
public void Constructor_ShouldThrowException_WhenHashIsNull()
{
Action action = () => new SwiftPMComponent("alamofire", "5.9.1", "https://github.com/Alamofire/Alamofire", null);
action.Should().Throw<ArgumentException>().WithMessage("*hash*");
}

[TestMethod]
public void PackageURL_ShouldReturnCorrectPackageURL()
{
var name = "alamofire";
var version = "5.9.1";
var packageUrl = "https://github.com/Alamofire/Alamofire";
var hash = "f455c2975872ccd2d9c81594c658af65716e9b9a";

var component = new SwiftPMComponent(name, version, packageUrl, hash);

var expectedPackageURL = new PackageURL(
type: "swift",
@namespace: "github.com",
name: name,
version: hash,
qualifiers: new SortedDictionary<string, string>
{
{ "repository_url", packageUrl },
},
subpath: null);

component.PackageURL.Should().BeEquivalentTo(expectedPackageURL);
}
}
Loading

0 comments on commit 7d05903

Please sign in to comment.