Skip to content

Commit

Permalink
Merge branch 'develop' into release/0.12.0
Browse files Browse the repository at this point in the history
* develop:
  Bump ApprovalTests from 5.4.7 to 5.5.0 in /src
  (#379) Validate old config file do not exist
  • Loading branch information
gep13 committed May 31, 2021
2 parents 7f1a9ae + 6e27969 commit a4b9ef4
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 20 deletions.
49 changes: 48 additions & 1 deletion src/GitReleaseManager.Core.Tests/Commands/InitCommandTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System;
using System.Globalization;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using GitReleaseManager.Core.Commands;
using GitReleaseManager.Core.Helpers;
Expand All @@ -24,7 +28,11 @@ public void Setup()
_fileSystem = new FileSystem(Substitute.For<BaseSubOptions>());
_logger = Substitute.For<ILogger>();
_command = new InitCommand(_fileSystem, _logger);
_targetDirectory = Path.GetTempPath();
_targetDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
if (!Directory.Exists(_targetDirectory))
{
Directory.CreateDirectory(_targetDirectory);
}
}

[Test]
Expand All @@ -46,5 +54,44 @@ public async Task Should_Execute_Command()
File.Delete(configFilePath);
}
}

[Test]
public async Task Should_Not_Execute_Command_For_Legacy_FileName()
{
var options = new InitSubOptions { TargetDirectory = _targetDirectory };

var configFilePath = Path.Combine(_targetDirectory, "GitReleaseManager.yaml");
var configNewFilePath = Path.Combine(_targetDirectory, "GitReleaseManager.yml");
if (File.Exists(configNewFilePath))
{
File.Delete(configNewFilePath);
}

File.WriteAllText(configFilePath, "s");
var expectedHash = GetHash(configFilePath);

var result = await _command.Execute(options).ConfigureAwait(false);
result.ShouldBe(0); // Should this perhaps return 1

var actualHash = GetHash(configFilePath);
actualHash.ShouldBe(expectedHash);
File.Exists(configNewFilePath).ShouldBeFalse();
}

private static string GetHash(string filePath)
{
using (var sha256 = SHA256.Create())
{
var bytes = sha256.ComputeHash(File.ReadAllBytes(filePath));

var builder = new StringBuilder();
foreach (var b in bytes)
{
builder.AppendFormat(CultureInfo.InvariantCulture, "{0:x2}", b);
}

return builder.ToString();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="ApprovalTests" Version="5.4.7" />
<PackageReference Include="ApprovalTests" Version="5.5.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
36 changes: 19 additions & 17 deletions src/GitReleaseManager.Core/Configuration/ConfigurationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,30 +64,32 @@ public static void WriteSample(string targetDirectory, IFileSystem fileSystem)

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

if (!fileSystem.Exists(defaultConfigFilePath))
foreach (var possiblePaths in new[] { defaultConfigFilePath, Path.Combine(targetDirectory, "GitReleaseManager.yaml") })
{
_logger.Information("Writing sample file to '{ConfigFilePath}'", defaultConfigFilePath);

// The following try/finally statements is to ensure that
// any stream is not disposed more than once.
Stream stream = null;
try
if (fileSystem.Exists(possiblePaths))
{
stream = fileSystem.OpenWrite(defaultConfigFilePath);
using (var writer = new StreamWriter(stream))
{
stream = null;
ConfigSerializer.WriteSample(writer);
}
_logger.Error("Cannot write sample, '{File}' already exists", possiblePaths);
return;
}
finally
}

_logger.Information("Writing sample file to '{ConfigFilePath}'", defaultConfigFilePath);

// The following try/finally statements is to ensure that
// any stream is not disposed more than once.
Stream stream = null;
try
{
stream = fileSystem.OpenWrite(defaultConfigFilePath);
using (var writer = new StreamWriter(stream))
{
stream?.Dispose();
stream = null;
ConfigSerializer.WriteSample(writer);
}
}
else
finally
{
_logger.Error("Cannot write sample, '{File}' already exists", defaultConfigFilePath);
stream?.Dispose();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/GitReleaseManager.Tests/GitReleaseManager.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="ApprovalTests" Version="5.4.7" />
<PackageReference Include="ApprovalTests" Version="5.5.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down

0 comments on commit a4b9ef4

Please sign in to comment.