Skip to content

Commit

Permalink
Asset IsZip
Browse files Browse the repository at this point in the history
  • Loading branch information
hlaueriksson committed Dec 13, 2024
1 parent 0a429b0 commit ecda0c0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/Community.PowerToys.Run.Plugin.Lint/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ public static GitHubOptions GetGitHubOptions(this string? url)
return reader.ReadToEnd();
}

public static bool IsZip(this Asset asset)
{
return
(asset?.content_type != null && asset.content_type.Contains("zip", StringComparison.OrdinalIgnoreCase)) ||
(asset?.name != null && asset.name.EndsWith(".zip", StringComparison.OrdinalIgnoreCase));
}

[GeneratedRegex(@"https:\/\/github.com\/([^\/]+)\/([^\/]+)$")]
private static partial Regex GitHubRegex();
}
2 changes: 1 addition & 1 deletion src/Community.PowerToys.Run.Plugin.Lint/ReleaseHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public async Task<Package[]> GetPackagesAsync()
using var client = new HttpClient();
foreach (var asset in release.assets)
{
if (!asset.content_type.Contains("zip", StringComparison.OrdinalIgnoreCase)) continue;
if (!asset.IsZip()) continue;

var path = Path.Combine(Path.GetTempPath(), asset.name);

Expand Down
2 changes: 1 addition & 1 deletion src/Community.PowerToys.Run.Plugin.Lint/Rules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public IEnumerable<string> Validate()
yield break;
}

if (!release.assets.Any(x => x.content_type.Contains("zip", StringComparison.OrdinalIgnoreCase)))
if (!release.assets.Any(x => x.IsZip()))
{
yield return $"Asset {".zip".ToQuote()} missing";
yield break;
Expand Down
11 changes: 11 additions & 0 deletions tests/Community.PowerToys.Run.Plugin.Lint.Tests/ExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,16 @@ public void GetEmbeddedResourceContent_should_return_file_content()
"".GetEmbeddedResourceContent().Should().BeNull();
((string)null!).GetEmbeddedResourceContent().Should().BeNull();
}

[Test]
public void IsZip_should_determine_if_Asset_is_zip_file()
{
new Asset { content_type = "application/x-zip-compressed" }.IsZip().Should().BeTrue();
new Asset { name = "Sample-0.2.0-x64.zip" }.IsZip().Should().BeTrue();
new Asset { name = "JetbrainsProjects-v1.0.1-x64.zip", content_type = "binary/octet-stream" }.IsZip().Should().BeTrue();
new Asset { name = "", content_type = "" }.IsZip().Should().BeFalse();
new Asset().IsZip().Should().BeFalse();
((Asset)null!).IsZip().Should().BeFalse();
}
}
}

0 comments on commit ecda0c0

Please sign in to comment.