Skip to content

Commit a4b9ef4

Browse files
committed
Merge branch 'develop' into release/0.12.0
* develop: Bump ApprovalTests from 5.4.7 to 5.5.0 in /src (#379) Validate old config file do not exist
2 parents 7f1a9ae + 6e27969 commit a4b9ef4

File tree

4 files changed

+69
-20
lines changed

4 files changed

+69
-20
lines changed

src/GitReleaseManager.Core.Tests/Commands/InitCommandTests.cs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
using System;
2+
using System.Globalization;
13
using System.IO;
4+
using System.Security.Cryptography;
5+
using System.Text;
26
using System.Threading.Tasks;
37
using GitReleaseManager.Core.Commands;
48
using GitReleaseManager.Core.Helpers;
@@ -24,7 +28,11 @@ public void Setup()
2428
_fileSystem = new FileSystem(Substitute.For<BaseSubOptions>());
2529
_logger = Substitute.For<ILogger>();
2630
_command = new InitCommand(_fileSystem, _logger);
27-
_targetDirectory = Path.GetTempPath();
31+
_targetDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
32+
if (!Directory.Exists(_targetDirectory))
33+
{
34+
Directory.CreateDirectory(_targetDirectory);
35+
}
2836
}
2937

3038
[Test]
@@ -46,5 +54,44 @@ public async Task Should_Execute_Command()
4654
File.Delete(configFilePath);
4755
}
4856
}
57+
58+
[Test]
59+
public async Task Should_Not_Execute_Command_For_Legacy_FileName()
60+
{
61+
var options = new InitSubOptions { TargetDirectory = _targetDirectory };
62+
63+
var configFilePath = Path.Combine(_targetDirectory, "GitReleaseManager.yaml");
64+
var configNewFilePath = Path.Combine(_targetDirectory, "GitReleaseManager.yml");
65+
if (File.Exists(configNewFilePath))
66+
{
67+
File.Delete(configNewFilePath);
68+
}
69+
70+
File.WriteAllText(configFilePath, "s");
71+
var expectedHash = GetHash(configFilePath);
72+
73+
var result = await _command.Execute(options).ConfigureAwait(false);
74+
result.ShouldBe(0); // Should this perhaps return 1
75+
76+
var actualHash = GetHash(configFilePath);
77+
actualHash.ShouldBe(expectedHash);
78+
File.Exists(configNewFilePath).ShouldBeFalse();
79+
}
80+
81+
private static string GetHash(string filePath)
82+
{
83+
using (var sha256 = SHA256.Create())
84+
{
85+
var bytes = sha256.ComputeHash(File.ReadAllBytes(filePath));
86+
87+
var builder = new StringBuilder();
88+
foreach (var b in bytes)
89+
{
90+
builder.AppendFormat(CultureInfo.InvariantCulture, "{0:x2}", b);
91+
}
92+
93+
return builder.ToString();
94+
}
95+
}
4996
}
5097
}

src/GitReleaseManager.Core.Tests/GitReleaseManager.Core.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<PrivateAssets>all</PrivateAssets>
1919
</PackageReference>
2020
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
21-
<PackageReference Include="ApprovalTests" Version="5.4.7" />
21+
<PackageReference Include="ApprovalTests" Version="5.5.0" />
2222
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0">
2323
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2424
<PrivateAssets>all</PrivateAssets>

src/GitReleaseManager.Core/Configuration/ConfigurationProvider.cs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -64,30 +64,32 @@ public static void WriteSample(string targetDirectory, IFileSystem fileSystem)
6464

6565
var defaultConfigFilePath = Path.Combine(targetDirectory, "GitReleaseManager.yml");
6666

67-
if (!fileSystem.Exists(defaultConfigFilePath))
67+
foreach (var possiblePaths in new[] { defaultConfigFilePath, Path.Combine(targetDirectory, "GitReleaseManager.yaml") })
6868
{
69-
_logger.Information("Writing sample file to '{ConfigFilePath}'", defaultConfigFilePath);
70-
71-
// The following try/finally statements is to ensure that
72-
// any stream is not disposed more than once.
73-
Stream stream = null;
74-
try
69+
if (fileSystem.Exists(possiblePaths))
7570
{
76-
stream = fileSystem.OpenWrite(defaultConfigFilePath);
77-
using (var writer = new StreamWriter(stream))
78-
{
79-
stream = null;
80-
ConfigSerializer.WriteSample(writer);
81-
}
71+
_logger.Error("Cannot write sample, '{File}' already exists", possiblePaths);
72+
return;
8273
}
83-
finally
74+
}
75+
76+
_logger.Information("Writing sample file to '{ConfigFilePath}'", defaultConfigFilePath);
77+
78+
// The following try/finally statements is to ensure that
79+
// any stream is not disposed more than once.
80+
Stream stream = null;
81+
try
82+
{
83+
stream = fileSystem.OpenWrite(defaultConfigFilePath);
84+
using (var writer = new StreamWriter(stream))
8485
{
85-
stream?.Dispose();
86+
stream = null;
87+
ConfigSerializer.WriteSample(writer);
8688
}
8789
}
88-
else
90+
finally
8991
{
90-
_logger.Error("Cannot write sample, '{File}' already exists", defaultConfigFilePath);
92+
stream?.Dispose();
9193
}
9294
}
9395

src/GitReleaseManager.Tests/GitReleaseManager.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<PrivateAssets>all</PrivateAssets>
1919
</PackageReference>
2020
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
21-
<PackageReference Include="ApprovalTests" Version="5.4.7" />
21+
<PackageReference Include="ApprovalTests" Version="5.5.0" />
2222
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0">
2323
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2424
<PrivateAssets>all</PrivateAssets>

0 commit comments

Comments
 (0)