Add comprehensive tests for Go and Python version conflict detection and licensing normalization
- Implemented GoVersionConflictDetectorTests to validate pseudo-version detection, conflict analysis, and conflict retrieval for Go modules. - Created VersionConflictDetectorTests for Python to assess conflict detection across various version scenarios, including major, minor, and patch differences. - Added SpdxLicenseNormalizerTests to ensure accurate normalization of SPDX license strings and classifiers. - Developed VendoredPackageDetectorTests to identify vendored packages and extract embedded packages from Python packages, including handling of vendor directories and known vendored packages.
This commit is contained in:
@@ -0,0 +1,226 @@
|
||||
using StellaOps.Scanner.Analyzers.Lang.Bun.Internal;
|
||||
|
||||
namespace StellaOps.Scanner.Analyzers.Lang.Bun.Tests.Parsers;
|
||||
|
||||
public sealed class BunConfigHelperTests
|
||||
{
|
||||
private readonly string _tempDir;
|
||||
|
||||
public BunConfigHelperTests()
|
||||
{
|
||||
_tempDir = Path.Combine(Path.GetTempPath(), $"bun-config-test-{Guid.NewGuid():N}");
|
||||
Directory.CreateDirectory(_tempDir);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (Directory.Exists(_tempDir))
|
||||
{
|
||||
Directory.Delete(_tempDir, recursive: true);
|
||||
}
|
||||
}
|
||||
|
||||
#region ParseConfig Tests
|
||||
|
||||
[Fact]
|
||||
public void ParseConfig_MissingFile_ReturnsEmpty()
|
||||
{
|
||||
var result = BunConfigHelper.ParseConfig(_tempDir);
|
||||
|
||||
Assert.Null(result.DefaultRegistry);
|
||||
Assert.Empty(result.ScopeRegistries);
|
||||
Assert.False(result.HasCustomRegistry);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseConfig_DefaultRegistry_ReturnsUrl()
|
||||
{
|
||||
var bunfig = """
|
||||
[install]
|
||||
registry = "https://npm.company.com/"
|
||||
""";
|
||||
File.WriteAllText(Path.Combine(_tempDir, "bunfig.toml"), bunfig);
|
||||
|
||||
var result = BunConfigHelper.ParseConfig(_tempDir);
|
||||
|
||||
Assert.Equal("https://npm.company.com/", result.DefaultRegistry);
|
||||
Assert.True(result.HasCustomRegistry);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseConfig_ScopedRegistries_ReturnsMappings()
|
||||
{
|
||||
var bunfig = """
|
||||
[install.scopes]
|
||||
"@company" = "https://npm.company.com/"
|
||||
"@internal" = "https://internal.registry.com/"
|
||||
""";
|
||||
File.WriteAllText(Path.Combine(_tempDir, "bunfig.toml"), bunfig);
|
||||
|
||||
var result = BunConfigHelper.ParseConfig(_tempDir);
|
||||
|
||||
Assert.Equal(2, result.ScopeRegistries.Count);
|
||||
Assert.Equal("https://npm.company.com/", result.ScopeRegistries["@company"]);
|
||||
Assert.Equal("https://internal.registry.com/", result.ScopeRegistries["@internal"]);
|
||||
Assert.True(result.HasCustomRegistry);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseConfig_InlineTableFormat_ExtractsUrl()
|
||||
{
|
||||
var bunfig = """
|
||||
[install.scopes]
|
||||
"@company" = { url = "https://npm.company.com/" }
|
||||
""";
|
||||
File.WriteAllText(Path.Combine(_tempDir, "bunfig.toml"), bunfig);
|
||||
|
||||
var result = BunConfigHelper.ParseConfig(_tempDir);
|
||||
|
||||
Assert.Single(result.ScopeRegistries);
|
||||
Assert.Equal("https://npm.company.com/", result.ScopeRegistries["@company"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseConfig_Comments_IgnoresComments()
|
||||
{
|
||||
var bunfig = """
|
||||
# This is a comment
|
||||
[install]
|
||||
# registry for npm packages
|
||||
registry = "https://npm.company.com/"
|
||||
""";
|
||||
File.WriteAllText(Path.Combine(_tempDir, "bunfig.toml"), bunfig);
|
||||
|
||||
var result = BunConfigHelper.ParseConfig(_tempDir);
|
||||
|
||||
Assert.Equal("https://npm.company.com/", result.DefaultRegistry);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseConfig_EmptyFile_ReturnsEmpty()
|
||||
{
|
||||
File.WriteAllText(Path.Combine(_tempDir, "bunfig.toml"), "");
|
||||
|
||||
var result = BunConfigHelper.ParseConfig(_tempDir);
|
||||
|
||||
Assert.Null(result.DefaultRegistry);
|
||||
Assert.Empty(result.ScopeRegistries);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParseConfig_BothDefaultAndScoped_ReturnsBoth()
|
||||
{
|
||||
var bunfig = """
|
||||
[install]
|
||||
registry = "https://default.registry.com/"
|
||||
|
||||
[install.scopes]
|
||||
"@company" = "https://npm.company.com/"
|
||||
""";
|
||||
File.WriteAllText(Path.Combine(_tempDir, "bunfig.toml"), bunfig);
|
||||
|
||||
var result = BunConfigHelper.ParseConfig(_tempDir);
|
||||
|
||||
Assert.Equal("https://default.registry.com/", result.DefaultRegistry);
|
||||
Assert.Single(result.ScopeRegistries);
|
||||
Assert.Equal("https://npm.company.com/", result.ScopeRegistries["@company"]);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region StripQuotes Tests
|
||||
|
||||
[Fact]
|
||||
public void StripQuotes_DoubleQuotes_RemovesQuotes()
|
||||
{
|
||||
var result = BunConfigHelper.StripQuotes("\"hello world\"");
|
||||
|
||||
Assert.Equal("hello world", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StripQuotes_SingleQuotes_RemovesQuotes()
|
||||
{
|
||||
var result = BunConfigHelper.StripQuotes("'hello world'");
|
||||
|
||||
Assert.Equal("hello world", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StripQuotes_NoQuotes_ReturnsUnchanged()
|
||||
{
|
||||
var result = BunConfigHelper.StripQuotes("hello world");
|
||||
|
||||
Assert.Equal("hello world", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StripQuotes_MismatchedQuotes_ReturnsUnchanged()
|
||||
{
|
||||
var result = BunConfigHelper.StripQuotes("\"hello world'");
|
||||
|
||||
Assert.Equal("\"hello world'", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StripQuotes_EmptyString_ReturnsEmpty()
|
||||
{
|
||||
var result = BunConfigHelper.StripQuotes("");
|
||||
|
||||
Assert.Equal("", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void StripQuotes_SingleCharacter_ReturnsUnchanged()
|
||||
{
|
||||
var result = BunConfigHelper.StripQuotes("a");
|
||||
|
||||
Assert.Equal("a", result);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region ExtractRegistryUrl Tests
|
||||
|
||||
[Fact]
|
||||
public void ExtractRegistryUrl_DirectUrl_ReturnsUrl()
|
||||
{
|
||||
var result = BunConfigHelper.ExtractRegistryUrl("https://npm.company.com/");
|
||||
|
||||
Assert.Equal("https://npm.company.com/", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ExtractRegistryUrl_InlineTable_ExtractsUrl()
|
||||
{
|
||||
var result = BunConfigHelper.ExtractRegistryUrl("{ url = \"https://npm.company.com/\" }");
|
||||
|
||||
Assert.Equal("https://npm.company.com/", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ExtractRegistryUrl_InlineTableSingleQuotes_ExtractsUrl()
|
||||
{
|
||||
var result = BunConfigHelper.ExtractRegistryUrl("{ url = 'https://npm.company.com/' }");
|
||||
|
||||
Assert.Equal("https://npm.company.com/", result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ExtractRegistryUrl_InvalidFormat_ReturnsNull()
|
||||
{
|
||||
var result = BunConfigHelper.ExtractRegistryUrl("not-a-url");
|
||||
|
||||
Assert.Null(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ExtractRegistryUrl_HttpUrl_ReturnsUrl()
|
||||
{
|
||||
var result = BunConfigHelper.ExtractRegistryUrl("http://internal.registry.local/");
|
||||
|
||||
Assert.Equal("http://internal.registry.local/", result);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user