Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,6 @@ namespace Microsoft.ComponentDetection.Contracts.TypedComponent;
/// </summary>
public class SwiftComponent : TypedComponent
{
Comment thread
ryanbrandenburg marked this conversation as resolved.
/// <summary>
/// Initializes a new instance of the <see cref="SwiftComponent"/> class.
/// </summary>
/// <param name="name">The name of the component.</param>
/// <param name="version">The version of the component.</param>
/// <param name="repositoryUrl">The source repository URL of the component.</param>
/// <param name="hash">The Git commit hash of the component.</param>
/// <param name="kind">The Swift package kind.</param>
public SwiftComponent(string name, string version, string repositoryUrl, string hash, string kind)
{
this.Name = this.ValidateRequiredInput(name, nameof(name), nameof(ComponentType.Swift));
this.Version = this.ValidateRequiredInput(version, nameof(version), nameof(ComponentType.Swift));
this.ValidateRequiredInput(repositoryUrl, nameof(repositoryUrl), nameof(ComponentType.Swift));
this.RepositoryUrl = new Uri(repositoryUrl);
this.Kind = this.ValidateRequiredInput(kind, nameof(kind), nameof(ComponentType.Swift));
this.CommitHash = this.ValidateRequiredInput(hash, nameof(hash), nameof(ComponentType.Swift));
}

public SwiftComponent()
{
/* Reserved for deserialization */
}

[JsonPropertyName("name")]
public string Name { get; set; }

Expand Down Expand Up @@ -68,7 +45,7 @@ public SwiftComponent()
},
subpath: null);

protected override string ComputeBaseId() => $"{this.RepositoryUrl.AbsoluteUri} {this.CommitHash} - {this.Type}";
protected override string ComputeBaseId() => $"{this.RepositoryUrl.AbsoluteUri} {this.Version} - {this.Type}";
Comment thread
ryanbrandenburg marked this conversation as resolved.

private string GetNamespaceFromPackageUrl()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public SwiftResolvedComponentDetector(

public override IEnumerable<ComponentType> SupportedComponentTypes => [ComponentType.Swift];

public override int Version => 2;
public override int Version => 3;

protected override Task OnFileFoundAsync(
ProcessRequest processRequest,
Expand Down Expand Up @@ -77,15 +77,19 @@ private void ProcessPackageResolvedFile(ISingleFileComponentRecorder singleFileC
ArgumentException.ThrowIfNullOrWhiteSpace(package.Identity);

// The version of the package is not always available.
var version = package.State.Version ?? package.State.Branch ?? package.State.Revision;

var detectedComponent = new SwiftComponent(
name: package.Identity,
version: version,
repositoryUrl: package.Location,
hash: package.State.Revision,
kind: package.Kind);
singleFileComponentRecorder.RegisterUsage(new DetectedComponent(component: detectedComponent));
// For now when there's no version we don't report.
if (!string.IsNullOrWhiteSpace(package.State.Version))
{
var detectedComponent = new SwiftComponent
{
Name = package.Identity,
Version = package.State.Version,
RepositoryUrl = new Uri(package.Location),
CommitHash = package.State.Revision,
Kind = package.Kind,
};
singleFileComponentRecorder.RegisterUsage(new DetectedComponent(component: detectedComponent));
}
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,14 @@ public void TypedComponent_Serialization_Git()
[TestMethod]
public void TypedComponent_Serialization_Swift()
{
TypedComponent component = new SwiftComponent(
"alamofire",
"5.9.1",
"https://github.com/Alamofire/Alamofire",
"f455c2975872ccd2d9c81594c658af65716e9b9a",
"remoteSourceControl");
TypedComponent component = new SwiftComponent
{
Name = "alamofire",
Version = "5.9.1",
RepositoryUrl = new Uri("https://github.com/Alamofire/Alamofire"),
CommitHash = "f455c2975872ccd2d9c81594c658af65716e9b9a",
Kind = "remoteSourceControl",
};

var json = JsonSerializer.Serialize(component);
var deserializedComponent = JsonSerializer.Deserialize<TypedComponent>(json);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ public void Constructor_ShouldInitializeProperties()
var kind = "remoteSourceControl";
var commitHash = "f455c2975872ccd2d9c81594c658af65716e9b9a";

var component = new SwiftComponent(name, version, repositoryUrl, commitHash, kind);
var component = new SwiftComponent
Comment thread
ryanbrandenburg marked this conversation as resolved.
{
Name = name,
Version = version,
RepositoryUrl = new Uri(repositoryUrl),
CommitHash = commitHash,
Kind = kind,
};

component.Name.Should().Be(name);
component.Version.Should().Be(version);
Expand All @@ -30,136 +37,51 @@ public void Constructor_ShouldInitializeProperties()
component.RepositoryUrl.Should().Be(new Uri(repositoryUrl));
component.Type.Should().Be(ComponentType.Swift);
component.Id.Should().Be(
$"{repositoryUrl} {commitHash} - {component.Type}");
$"{repositoryUrl} {version} - {component.Type}");
}

[TestMethod]
public void Id_ShouldDistinguishRepositoriesAndCommitHashes()
public void Id_ShouldDistinguishRepositories()
{
var component = new SwiftComponent(
"alamofire",
"5.9.1",
"https://github.com/Alamofire/Alamofire",
"f455c2975872ccd2d9c81594c658af65716e9b9a",
"remoteSourceControl");
var differentRepository = new SwiftComponent(
"alamofire",
"5.9.1",
"https://github.com/example/Alamofire",
"f455c2975872ccd2d9c81594c658af65716e9b9a",
"remoteSourceControl");
var differentCommit = new SwiftComponent(
"alamofire",
"5.9.1",
"https://github.com/Alamofire/Alamofire",
"63dfa86548c4e5d5c6fd6ed42f638e388cbce529",
"remoteSourceControl");
var component = new SwiftComponent
{
Name = "alamofire",
Version = "5.9.1",
RepositoryUrl = new Uri("https://github.com/Alamofire/Alamofire"),
CommitHash = "f455c2975872ccd2d9c81594c658af65716e9b9a",
Kind = "remoteSourceControl",
};
var differentRepository = new SwiftComponent
{
Name = "alamofire",
Version = "5.9.1",
RepositoryUrl = new Uri("https://github.com/example/Alamofire"),
CommitHash = "f455c2975872ccd2d9c81594c658af65716e9b9a",
Kind = "remoteSourceControl",
};

component.Id.Should().NotBe(differentRepository.Id);
component.Id.Should().NotBe(differentCommit.Id);
}

[TestMethod]
public void Serialization_ShouldIncludeCommitHash()
{
var commitHash = "f455c2975872ccd2d9c81594c658af65716e9b9a";
TypedComponent component = new SwiftComponent(
name: "alamofire",
version: "5.9.1",
repositoryUrl: "https://github.com/Alamofire/Alamofire",
hash: commitHash,
kind: "remoteSourceControl");
TypedComponent component = new SwiftComponent
{
Name = "alamofire",
Version = "5.9.1",
RepositoryUrl = new Uri("https://github.com/Alamofire/Alamofire"),
CommitHash = commitHash,
Kind = "remoteSourceControl",
};

using var json = JsonDocument.Parse(JsonSerializer.Serialize(component));

json.RootElement.GetProperty("kind").GetString().Should().Be("remoteSourceControl");
json.RootElement.GetProperty("commitHash").GetString().Should().Be(commitHash);
}

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

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

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

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

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

[TestMethod]
public void Constructor_ShouldThrowException_WhenRepositoryUrlIsInvalid()
{
Action action = () =>
new SwiftComponent(
"alamofire",
"5.9.1",
"invalid-url",
"f455c2975872ccd2d9c81594c658af65716e9b9a",
"remoteSourceControl"
);
action.Should().Throw<UriFormatException>();
}

[TestMethod]
public void PackageURL_ShouldReturnCorrectPackageURL_GithubHostname()
{
Expand All @@ -168,7 +90,14 @@ public void PackageURL_ShouldReturnCorrectPackageURL_GithubHostname()
var repositoryUrl = "https://github.com/Alamofire/Alamofire";
var hash = "f455c2975872ccd2d9c81594c658af65716e9b9a";

var component = new SwiftComponent(name, version, repositoryUrl, hash, "remoteSourceControl");
var component = new SwiftComponent
{
Name = name,
Version = version,
RepositoryUrl = new Uri(repositoryUrl),
CommitHash = hash,
Kind = "remoteSourceControl",
};

var expectedPackageURL = new PackageURL(
type: "swift",
Expand All @@ -190,7 +119,14 @@ public void PackageURL_ShouldReturnCorrectPackageURL_GithubHostname_Alternate()
var repositoryUrl = "https://giTHub.com/Alamofire/Alamofire";
var hash = "f455c2975872ccd2d9c81594c658af65716e9b9a";

var component = new SwiftComponent(name, version, repositoryUrl, hash, "remoteSourceControl");
var component = new SwiftComponent
{
Name = name,
Version = version,
RepositoryUrl = new Uri(repositoryUrl),
CommitHash = hash,
Kind = "remoteSourceControl",
};

var expectedPackageURL = new PackageURL(
type: "swift",
Expand All @@ -215,7 +151,14 @@ public void PackageURL_ShouldReturnCorrectPackageURL_OtherHostname()
var repositoryUrl = "https://otherhostname.com/Alamofire/Alamofire";
var hash = "f455c2975872ccd2d9c81594c658af65716e9b9a";

var component = new SwiftComponent(name, version, repositoryUrl, hash, "remoteSourceControl");
var component = new SwiftComponent
{
Name = name,
Version = version,
RepositoryUrl = new Uri(repositoryUrl),
CommitHash = hash,
Kind = "remoteSourceControl",
};

var expectedPackageURL = new PackageURL(
type: "swift",
Expand Down
Loading
Loading