-
Notifications
You must be signed in to change notification settings - Fork 4
feat(tests): Add Nodsoft.MoltenObsidian.Tool.Tests covering CLI commands end-to-end #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
3
commits into
develop
Choose a base branch
from
copilot/add-cli-tool-tests
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
Nodsoft.MoltenObsidian.Tool.Tests/Commands/Manifest/GenerateManifestCommandTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| using Nodsoft.MoltenObsidian.Manifest; | ||
| using Nodsoft.MoltenObsidian.Tool.Commands.Manifest; | ||
| using Nodsoft.MoltenObsidian.Vaults.FileSystem; | ||
| using Nodsoft.MoltenObsidian.Vaults.InMemory; | ||
|
|
||
| namespace Nodsoft.MoltenObsidian.Tool.Tests.Commands.Manifest; | ||
|
|
||
| /// <summary> | ||
| /// Provides unit tests for <see cref="GenerateManifestCommand"/> logic. | ||
| /// </summary> | ||
| public sealed class GenerateManifestCommandTests : IDisposable | ||
| { | ||
| private readonly VaultFixture _fixture; | ||
| private readonly DirectoryInfo _outputDir; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of <see cref="GenerateManifestCommandTests"/>. | ||
| /// </summary> | ||
| public GenerateManifestCommandTests() | ||
| { | ||
| _fixture = new VaultFixture(); | ||
| _outputDir = Directory.CreateTempSubdirectory("moltenobsidian-manifest-output-"); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public void Dispose() | ||
| { | ||
| _fixture.Dispose(); | ||
| _outputDir.Delete(recursive: true); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// <see cref="GenerateManifestCommand.GenerateManifestAsync"/> writes a manifest file at the output path. | ||
| /// </summary> | ||
| [Fact] | ||
| public async Task GenerateManifestAsync_WritesManifestFile() | ||
| { | ||
| await GenerateManifestCommand.GenerateManifestAsync( | ||
| _fixture.Vault, | ||
| _fixture.VaultDirectory, | ||
| _outputDir, | ||
| debugMode: false, | ||
| promptOverwrite: _ => true, | ||
| ct: TestContext.Current.CancellationToken | ||
| ); | ||
|
|
||
| string manifestPath = Path.Combine(_outputDir.FullName, RemoteVaultManifest.ManifestFileName); | ||
| Assert.True(File.Exists(manifestPath)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// The manifest file written by <see cref="GenerateManifestCommand.GenerateManifestAsync"/> contains the seeded vault files. | ||
| /// </summary> | ||
| [Fact] | ||
| public async Task GenerateManifestAsync_ManifestContainsVaultFiles() | ||
| { | ||
| RemoteVaultManifest manifest = await GenerateManifestCommand.GenerateManifestAsync( | ||
| _fixture.Vault, | ||
| _fixture.VaultDirectory, | ||
| _outputDir, | ||
| debugMode: false, | ||
| promptOverwrite: _ => true, | ||
| ct: TestContext.Current.CancellationToken | ||
| ); | ||
|
|
||
| Assert.NotEmpty(manifest.Files); | ||
| Assert.Contains(manifest.Files, f => f.Path.EndsWith("Note.md")); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// When the manifest file already exists and the overwrite prompt returns <c>false</c>, | ||
| /// the existing file is left untouched. | ||
| /// </summary> | ||
| [Fact] | ||
| public async Task GenerateManifestAsync_ExistingFile_PromptReturnsFalse_FileNotOverwritten() | ||
| { | ||
| string manifestPath = Path.Combine(_outputDir.FullName, RemoteVaultManifest.ManifestFileName); | ||
| const string originalContent = "original-content"; | ||
| await File.WriteAllTextAsync(manifestPath, originalContent, TestContext.Current.CancellationToken); | ||
|
|
||
| await GenerateManifestCommand.GenerateManifestAsync( | ||
| _fixture.Vault, | ||
| _fixture.VaultDirectory, | ||
| _outputDir, | ||
| debugMode: false, | ||
| promptOverwrite: _ => false, | ||
| ct: TestContext.Current.CancellationToken | ||
| ); | ||
|
|
||
| Assert.Equal(originalContent, await File.ReadAllTextAsync(manifestPath, TestContext.Current.CancellationToken)); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// When the manifest file already exists and the overwrite prompt returns <c>true</c>, | ||
| /// the file is overwritten with a fresh manifest. | ||
| /// </summary> | ||
| [Fact] | ||
| public async Task GenerateManifestAsync_ExistingFile_PromptReturnsTrue_FileOverwritten() | ||
| { | ||
| string manifestPath = Path.Combine(_outputDir.FullName, RemoteVaultManifest.ManifestFileName); | ||
| const string originalContent = "original-content"; | ||
| await File.WriteAllTextAsync(manifestPath, originalContent, TestContext.Current.CancellationToken); | ||
|
|
||
| await GenerateManifestCommand.GenerateManifestAsync( | ||
| _fixture.Vault, | ||
| _fixture.VaultDirectory, | ||
| _outputDir, | ||
| debugMode: false, | ||
| promptOverwrite: _ => true, | ||
| ct: TestContext.Current.CancellationToken | ||
| ); | ||
|
|
||
| string newContent = await File.ReadAllTextAsync(manifestPath, TestContext.Current.CancellationToken); | ||
| Assert.NotEqual(originalContent, newContent); | ||
| // The overwritten file should contain valid JSON (starts with '{') | ||
| Assert.StartsWith("{", newContent.TrimStart()); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Passing a non-<see cref="FileSystemVault"/> to <see cref="GenerateManifestCommand.GenerateManifestAsync"/> | ||
| /// throws <see cref="InvalidOperationException"/>. | ||
| /// </summary> | ||
| [Fact] | ||
| public async Task GenerateManifestAsync_NonFileSystemVault_ThrowsInvalidOperationException() | ||
| { | ||
| InMemoryVault inMemoryVault = new("TestVault"); | ||
|
|
||
| await Assert.ThrowsAsync<InvalidOperationException>(() => | ||
| GenerateManifestCommand.GenerateManifestAsync( | ||
| inMemoryVault, | ||
| _fixture.VaultDirectory, | ||
| _outputDir, | ||
| debugMode: false, | ||
| promptOverwrite: _ => true, | ||
| ct: TestContext.Current.CancellationToken | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
131 changes: 131 additions & 0 deletions
131
Nodsoft.MoltenObsidian.Tool.Tests/Commands/Manifest/GenerateManifestSettingsTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| using Nodsoft.MoltenObsidian.Tool.Commands.Manifest; | ||
|
|
||
| namespace Nodsoft.MoltenObsidian.Tool.Tests.Commands.Manifest; | ||
|
|
||
| /// <summary> | ||
| /// Provides tests for <see cref="GenerateManifestSettings.Validate"/>. | ||
| /// </summary> | ||
| public sealed class GenerateManifestSettingsTests : IDisposable | ||
| { | ||
| private readonly DirectoryInfo _vaultDir; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of <see cref="GenerateManifestSettingsTests"/>, | ||
| /// creating a temp vault directory with the required <c>.obsidian</c> marker. | ||
| /// </summary> | ||
| public GenerateManifestSettingsTests() | ||
| { | ||
| _vaultDir = Directory.CreateTempSubdirectory("moltenobsidian-settings-test-"); | ||
| Directory.CreateDirectory(Path.Combine(_vaultDir.FullName, ".obsidian")); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public void Dispose() => _vaultDir.Delete(recursive: true); | ||
|
|
||
| /// <summary> | ||
| /// A valid vault path that contains a <c>.obsidian</c> folder passes validation. | ||
| /// </summary> | ||
| [Fact] | ||
| public void Validate_ValidVaultPath_ReturnsSuccess() | ||
| { | ||
| GenerateManifestSettings settings = new() { VaultPathStr = _vaultDir.FullName }; | ||
| Assert.True(settings.Validate().Successful); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// An empty vault path string is rejected. | ||
| /// </summary> | ||
| [Fact] | ||
| public void Validate_EmptyVaultPath_ReturnsError() | ||
| { | ||
| GenerateManifestSettings settings = new() { VaultPathStr = "" }; | ||
| Assert.False(settings.Validate().Successful); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// A vault path that does not exist on disk is rejected. | ||
| /// </summary> | ||
| [Fact] | ||
| public void Validate_NonExistentVaultPath_ReturnsError() | ||
| { | ||
| GenerateManifestSettings settings = new() { VaultPathStr = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()) }; | ||
| Assert.False(settings.Validate().Successful); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// A vault directory without <c>.obsidian</c> and <c>Force = false</c> is rejected. | ||
| /// </summary> | ||
| [Fact] | ||
| public void Validate_NoObsidianFolder_ForceOff_ReturnsError() | ||
| { | ||
| DirectoryInfo plainDir = Directory.CreateTempSubdirectory("moltenobsidian-plain-"); | ||
|
|
||
| try | ||
| { | ||
| GenerateManifestSettings settings = new() { VaultPathStr = plainDir.FullName, Force = false }; | ||
| Assert.False(settings.Validate().Successful); | ||
| } | ||
| finally | ||
| { | ||
| plainDir.Delete(recursive: true); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// A vault directory without <c>.obsidian</c> but <c>Force = true</c> passes validation. | ||
| /// </summary> | ||
| [Fact] | ||
| public void Validate_NoObsidianFolder_ForceOn_ReturnsSuccess() | ||
| { | ||
| DirectoryInfo plainDir = Directory.CreateTempSubdirectory("moltenobsidian-plain-"); | ||
|
|
||
| try | ||
| { | ||
| GenerateManifestSettings settings = new() { VaultPathStr = plainDir.FullName, Force = true }; | ||
| Assert.True(settings.Validate().Successful); | ||
| } | ||
| finally | ||
| { | ||
| plainDir.Delete(recursive: true); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// A valid vault path combined with a valid existing output path passes validation. | ||
| /// </summary> | ||
| [Fact] | ||
| public void Validate_ValidVaultAndExistingOutputPath_ReturnsSuccess() | ||
| { | ||
| DirectoryInfo outputDir = Directory.CreateTempSubdirectory("moltenobsidian-output-"); | ||
|
|
||
| try | ||
| { | ||
| GenerateManifestSettings settings = new() | ||
| { | ||
| VaultPathStr = _vaultDir.FullName, | ||
| OutputPathStr = outputDir.FullName, | ||
| }; | ||
|
|
||
| Assert.True(settings.Validate().Successful); | ||
| } | ||
| finally | ||
| { | ||
| outputDir.Delete(recursive: true); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// A valid vault path combined with a non-existent output path is rejected. | ||
| /// </summary> | ||
| [Fact] | ||
| public void Validate_ValidVaultAndNonExistentOutputPath_ReturnsError() | ||
| { | ||
| GenerateManifestSettings settings = new() | ||
| { | ||
| VaultPathStr = _vaultDir.FullName, | ||
| OutputPathStr = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()), | ||
| }; | ||
|
|
||
| Assert.False(settings.Validate().Successful); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The assertion
EndsWith("Note.md")is ambiguous here because multiple seeded files end with the same suffix (e.g., "Another Note.md"). This can let the test pass even if the specific expected file is missing. Prefer asserting an exact relative path match (or asserting the full expected set of paths) to avoid false positives.