Skip to content

Commit

Permalink
feat: lip list
Browse files Browse the repository at this point in the history
  • Loading branch information
futrime committed Jan 20, 2025
1 parent d6739c0 commit 4835472
Show file tree
Hide file tree
Showing 18 changed files with 409 additions and 61 deletions.
2 changes: 1 addition & 1 deletion Lip.Tests/LipConfigTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Lip.Tests;
public class LipConfigTests
{
private static readonly string s_runtimeConfigPath = Path.Join(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "lip", "runtime_config.json");
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "lip", "liprc.json");

[Fact]
public async Task ConfigDelete_SingleItem_ResetsToDefault()
Expand Down
280 changes: 280 additions & 0 deletions Lip.Tests/LipListTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
using System.IO.Abstractions.TestingHelpers;
using System.Runtime.InteropServices;
using Microsoft.Extensions.Logging;
using Moq;

namespace Lip.Tests;

public class LipListTests
{
[Fact]
public async Task List_ReturnsListItems()
{
RuntimeConfig initialRuntimeConfig = new();

// Arrange.
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ "tooth_lock.json", new MockFileData($$"""
{
"format_version": 3,
"format_uuid": "289f771f-2c9a-4d73-9f3f-8492495a924d",
"packages": [
{
"format_version": 3,
"format_uuid": "289f771f-2c9a-4d73-9f3f-8492495a924d",
"tooth": "example.com/pkg1",
"version": "1.0.0",
"variants": [
{
"label": "variant1",
"platform": "{{RuntimeInformation.RuntimeIdentifier}}"
}
]
},
{
"format_version": 3,
"format_uuid": "289f771f-2c9a-4d73-9f3f-8492495a924d",
"tooth": "example.com/pkg2",
"version": "1.0.1",
"variants": [
{
"label": "variant2",
"platform": "{{RuntimeInformation.RuntimeIdentifier}}"
}
]
}
],
"locks": [
{
"tooth": "example.com/pkg1",
"variant": "variant1",
"version": "1.0.0",
}
]
}
""") }
});

Mock<ILogger> logger = new();

Mock<IPathManager> pathManager = new();
pathManager.SetupGet(m => m.PackageLockPath).Returns("tooth_lock.json");

Mock<IUserInteraction> userInteraction = new();

Lip lip = new(
initialRuntimeConfig,
fileSystem,
logger.Object,
pathManager.Object,
userInteraction.Object,
runtimeIdentifier: RuntimeInformation.RuntimeIdentifier);

// Act.
List<Lip.ListItem> listItems = await lip.List(new());

// Assert.
Assert.Equal(2, listItems.Count);
Assert.Equal("example.com/pkg1", listItems[0].Manifest.ToothPath);
Assert.Equal("1.0.0", listItems[0].Manifest.VersionText.ToString());
Assert.Equal("variant1", listItems[0].Manifest.Variants![0].VariantLabel);
Assert.True(listItems[0].Locked);
Assert.Equal("example.com/pkg2", listItems[1].Manifest.ToothPath);
Assert.Equal("1.0.1", listItems[1].Manifest.VersionText.ToString());
Assert.Equal("variant2", listItems[1].Manifest.Variants![0].VariantLabel);
Assert.False(listItems[1].Locked);
}

[Fact]
public async Task List_LockFileNotExists_ReturnsEmptyList()
{
RuntimeConfig initialRuntimeConfig = new();

// Arrange.
var fileSystem = new MockFileSystem();

Mock<ILogger> logger = new();

Mock<IPathManager> pathManager = new();
pathManager.SetupGet(m => m.PackageLockPath).Returns("tooth_lock.json");

Mock<IUserInteraction> userInteraction = new();

Lip lip = new(initialRuntimeConfig, fileSystem, logger.Object, pathManager.Object, userInteraction.Object);

// Act.
List<Lip.ListItem> listItems = await lip.List(new());

// Assert.
Assert.Empty(listItems);
}

[Fact]
public async Task List_MismatchedToothPath_ReturnsListItems()
{
RuntimeConfig initialRuntimeConfig = new();

// Arrange.
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ "tooth_lock.json", new MockFileData("""
{
"format_version": 3,
"format_uuid": "289f771f-2c9a-4d73-9f3f-8492495a924d",
"packages": [
{
"format_version": 3,
"format_uuid": "289f771f-2c9a-4d73-9f3f-8492495a924d",
"tooth": "example.com/pkg1",
"version": "1.0.0",
"variants": [
{
"label": "variant1"
}
]
}
],
"locks": [
{
"tooth": "example.com/pkg2",
"variant": "variant1",
"version": "1.0.0",
}
]
}
""") }
});

Mock<ILogger> logger = new();

Mock<IPathManager> pathManager = new();
pathManager.SetupGet(m => m.PackageLockPath).Returns("tooth_lock.json");

Mock<IUserInteraction> userInteraction = new();

Lip lip = new(initialRuntimeConfig, fileSystem, logger.Object, pathManager.Object, userInteraction.Object);

// Act.
List<Lip.ListItem> listItems = await lip.List(new());

// Assert.
Assert.Single(listItems);
Assert.Equal("example.com/pkg1", listItems[0].Manifest.ToothPath);
Assert.Equal("1.0.0", listItems[0].Manifest.Version.ToString());
Assert.Equal("variant1", listItems[0].Manifest.Variants![0].VariantLabel);
Assert.False(listItems[0].Locked);
}

[Fact]
public async Task List_MismatchedVersion_ReturnsListItems()
{
RuntimeConfig initialRuntimeConfig = new();

// Arrange.
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ "tooth_lock.json", new MockFileData("""
{
"format_version": 3,
"format_uuid": "289f771f-2c9a-4d73-9f3f-8492495a924d",
"packages": [
{
"format_version": 3,
"format_uuid": "289f771f-2c9a-4d73-9f3f-8492495a924d",
"tooth": "example.com/pkg1",
"version": "1.0.0",
"variants": [
{
"label": "variant1"
}
]
}
],
"locks": [
{
"tooth": "example.com/pkg1",
"variant": "variant1",
"version": "1.0.1",
}
]
}
""") }
});

Mock<ILogger> logger = new();

Mock<IPathManager> pathManager = new();
pathManager.SetupGet(m => m.PackageLockPath).Returns("tooth_lock.json");

Mock<IUserInteraction> userInteraction = new();

Lip lip = new(initialRuntimeConfig, fileSystem, logger.Object, pathManager.Object, userInteraction.Object);

// Act.
List<Lip.ListItem> listItems = await lip.List(new());

// Assert.
Assert.Single(listItems);
Assert.Equal("example.com/pkg1", listItems[0].Manifest.ToothPath);
Assert.Equal("1.0.0", listItems[0].Manifest.Version.ToString());
Assert.Equal("variant1", listItems[0].Manifest.Variants![0].VariantLabel);
Assert.False(listItems[0].Locked);
}

[Fact]
public async Task List_MismatchedVariantLabel_ReturnsListItems()
{
RuntimeConfig initialRuntimeConfig = new();

// Arrange.
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ "tooth_lock.json", new MockFileData("""
{
"format_version": 3,
"format_uuid": "289f771f-2c9a-4d73-9f3f-8492495a924d",
"packages": [
{
"format_version": 3,
"format_uuid": "289f771f-2c9a-4d73-9f3f-8492495a924d",
"tooth": "example.com/pkg1",
"version": "1.0.0",
"variants": [
{
"label": "variant1"
}
]
}
],
"locks": [
{
"tooth": "example.com/pkg1",
"variant": "variant2",
"version": "1.0.0",
}
]
}
""") }
});

Mock<ILogger> logger = new();

Mock<IPathManager> pathManager = new();
pathManager.SetupGet(m => m.PackageLockPath).Returns("tooth_lock.json");

Mock<IUserInteraction> userInteraction = new();

Lip lip = new(initialRuntimeConfig, fileSystem, logger.Object, pathManager.Object, userInteraction.Object);

// Act.
List<Lip.ListItem> listItems = await lip.List(new());

// Assert.
Assert.Single(listItems);
Assert.Equal("example.com/pkg1", listItems[0].Manifest.ToothPath);
Assert.Equal("1.0.0", listItems[0].Manifest.Version.ToString());
Assert.Equal("variant1", listItems[0].Manifest.Variants![0].VariantLabel);
Assert.False(listItems[0].Locked);
}
}
21 changes: 14 additions & 7 deletions Lip.Tests/PackageLockTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Text;
using System.Text.Json;
using Semver;

namespace Lip.Tests;

Expand Down Expand Up @@ -63,7 +64,13 @@ public void FromBytes_MaximumJson_Passes()
Assert.Equal(3, lockFile.FormatVersion);
Assert.Equal("289f771f-2c9a-4d73-9f3f-8492495a924d", lockFile.FormatUuid);
Assert.Single(lockFile.Packages);
Assert.Equal("example.com/pkg", lockFile.Packages[0].ToothPath);
Assert.Equal("1.0.0", lockFile.Packages[0].VersionText);
Assert.Single(lockFile.Locks);
Assert.Equal("example.com/pkg", lockFile.Locks[0].ToothPath);
Assert.Equal("default", lockFile.Locks[0].VariantLabel);
Assert.Equal("1.0.0", lockFile.Locks[0].VersionText);
Assert.Equal(SemVersion.Parse("1.0.0"), lockFile.Locks[0].Version);
}

[Fact]
Expand Down Expand Up @@ -161,14 +168,14 @@ public void ToBytes_MaximumJson_Passes()
FormatVersion = 3,
FormatUuid = "289f771f-2c9a-4d73-9f3f-8492495a924d",
ToothPath = "example.com/pkg",
Version = "1.0.0"
VersionText = "1.0.0"
}
],
Locks = [
new() {
ToothPath = "example.com/pkg",
VariantLabel = "default",
Version = "1.0.0"
VersionText = "1.0.0"
}
]
};
Expand Down Expand Up @@ -208,13 +215,13 @@ public void LockType_Constructor_ValidValues_Passes()
{
ToothPath = "example.com/package",
VariantLabel = "default",
Version = "1.0.0"
VersionText = "1.0.0"
};

// Assert
Assert.Equal("example.com/package", lockType.ToothPath);
Assert.Equal("default", lockType.VariantLabel);
Assert.Equal("1.0.0", lockType.Version);
Assert.Equal("1.0.0", lockType.VersionText);
}

[Fact]
Expand All @@ -225,7 +232,7 @@ public void LockType_Constructor_InvalidToothPath_Throws()
{
ToothPath = "invalid/tooth",
VariantLabel = "default",
Version = "1.0.0"
VersionText = "1.0.0"
});
Assert.Equal("Invalid tooth path 'invalid/tooth'.", exception.Message);
}
Expand All @@ -238,7 +245,7 @@ public void LockType_Constructor_InvalidVariantLabel_Throws()
{
ToothPath = "example.com/package",
VariantLabel = "invalid-variant",
Version = "1.0.0"
VersionText = "1.0.0"
});
Assert.Equal("Invalid variant label 'invalid-variant'.", exception.Message);
}
Expand All @@ -251,7 +258,7 @@ public void LockType_Constructor_InvalidVersion_Throws()
{
ToothPath = "example.com/package",
VariantLabel = "default",
Version = "invalid-version"
VersionText = "invalid-version"
});
Assert.Equal("Invalid version 'invalid-version'.", exception.Message);
}
Expand Down
Loading

0 comments on commit 4835472

Please sign in to comment.