Skip to content

Commit

Permalink
test: Refactor integration test ready to add more
Browse files Browse the repository at this point in the history
Looked at adding unit tests but requires to much filesystem mocking,
wasn't worth it.
  • Loading branch information
timabell committed Dec 22, 2023
1 parent 4ac92f8 commit c45909b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 22 deletions.
13 changes: 11 additions & 2 deletions tests/FakeGuidGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@ namespace tests;

public class FakeGuidGenerator : IGuidGenerator
{
public readonly Queue<Guid> Guids = new();
public FakeGuidGenerator(IEnumerable<string> guidsToReturn)
{
foreach (var guid in guidsToReturn)
{
_guids.Enqueue(new Guid(guid));
}
}

private readonly Queue<Guid> _guids = new();

public Guid Next()
{
return Guids.Dequeue();
return _guids.Dequeue();
}
}
82 changes: 62 additions & 20 deletions tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,35 @@

namespace tests;

public class IntegrationTests(ITestOutputHelper output)
public class IntegrationTests
{
private readonly CLI _cli;
private readonly string _testFolder;

private static readonly string[] GuidsToReturn =
{
// deterministic GUID list for tests so we can assert against a fixed expected sln contents
"17591C35-3F90-4F4A-AA13-45CF8D824066",
"CF942CDD-19AC-4E52-9C6E-B1381E0406D9",
"F5636E74-888A-4FBD-A8E2-9718A05D90BD",
"D6CA39BB-4B2F-4AF7-94B9-D1269AE037D3",
"5B009B7F-333C-469A-AB3D-24E29C18C544",
};

public IntegrationTests(ITestOutputHelper output)
{
_cli = new CLI(new FakeGuidGenerator(GuidsToReturn));
_testFolder = Path.Combine(Path.GetTempPath(), "sln-items-sync-tests", Guid.NewGuid().ToString());
output.WriteLine($"Test filesystem path:\r\n{_testFolder}");
Directory.CreateDirectory(_testFolder);
Directory.SetCurrentDirectory(_testFolder);
}

[Fact]
public void ModifiesSln()
public void AddsToBlankSln()
{
var tmp = Path.Combine(Path.GetTempPath(), "sln-items-sync-tests", Guid.NewGuid().ToString());
output.WriteLine($"Temp path:\r\n{tmp}");
Directory.CreateDirectory(tmp);
Directory.SetCurrentDirectory(tmp);
const string sln = @"
// Arrange
SetupSln(@"
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
Expand All @@ -29,18 +48,18 @@ public void ModifiesSln()
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
";
File.WriteAllText(Path.Combine(tmp, "original.sln"), sln);
File.WriteAllText(Path.Combine(tmp, "target.sln"), sln);
File.WriteAllText(Path.Combine(tmp, "rootfile.txt"), "");
Directory.CreateDirectory(Path.Combine(tmp, "subfolder", "nested_folder"));
File.WriteAllText(Path.Combine(tmp, "subfolder", "nested_folder", "nested_file.txt"), "");
");

SetupFilesystem(new[]
{
"rootfile.txt",
"subfolder/nested_folder/nested_file.txt",
});

var fakeGuidGenerator = new FakeGuidGenerator();
fakeGuidGenerator.Guids.Enqueue(new Guid("{17591C35-3F90-4F4A-AA13-45CF8D824066}"));
fakeGuidGenerator.Guids.Enqueue(new Guid("{CF942CDD-19AC-4E52-9C6E-B1381E0406D9}"));
fakeGuidGenerator.Guids.Enqueue(new Guid("{F5636E74-888A-4FBD-A8E2-9718A05D90BD}"));
// Act
_cli.Run(new[] { "-s", "target.sln", "rootfile.txt", "subfolder" });

// Assert
const string expected = @"
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
Expand Down Expand Up @@ -75,9 +94,32 @@ public void ModifiesSln()
EndGlobal
";

new CLI(fakeGuidGenerator).Run(new[] { "-s", "target.sln", "rootfile.txt", "subfolder" });

var actual = File.ReadAllText(Path.Combine(tmp, "target.sln"));
var actual = File.ReadAllText(Path.Combine(_testFolder, "target.sln"));
actual.Should().Be(expected);
}

private void SetupFilesystem(IEnumerable<string> paths)
{
foreach (var path in paths)
{
SetupFile(path);
}
}

private void SetupFile(string path)
{
var directory = Path.GetDirectoryName(path);
if (!string.IsNullOrEmpty(directory))
{
Directory.CreateDirectory(directory);
}

File.WriteAllText(Path.Combine(_testFolder, path), contents: "");
}

private void SetupSln(string slnContents)
{
File.WriteAllText(Path.Combine(_testFolder, "original.sln"), slnContents);
File.WriteAllText(Path.Combine(_testFolder, "target.sln"), slnContents);
}
}

0 comments on commit c45909b

Please sign in to comment.