Skip to content

Commit

Permalink
fix: changelog path option overwrites CHANGELOG.md if multiple projec…
Browse files Browse the repository at this point in the history
…ts point to the same location (#148)

Detect duplicate changelog paths during config read.
Inform the user and exit with an error message.

Introduce new config folder tests with
ConfigProviderTests.cs
  • Loading branch information
Barsan1 authored Oct 19, 2024
1 parent e11e097 commit 4fb2ffa
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
68 changes: 68 additions & 0 deletions Versionize.Tests/Config/ConfigProviderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using McMaster.Extensions.CommandLineUtils;
using Newtonsoft.Json;
using Shouldly;
using Versionize.CommandLine;
using Versionize.Config;
using Versionize.Tests.TestSupport;
using Xunit;

namespace Versionize.Tests.Config;

public class ConfigProviderTests : IDisposable
{
private readonly TestSetup _testSetup;
private readonly TestPlatformAbstractions _testPlatformAbstractions;

public ConfigProviderTests()
{
_testSetup = TestSetup.Create();

_testPlatformAbstractions = new TestPlatformAbstractions();
CommandLineUI.Platform = _testPlatformAbstractions;
}

[Fact]
public void ShouldExitIfChangelogPathsPointingToSameLocation()
{
var projects = new[]
{
new ProjectOptions
{
Name = "Project1",
Path = "project1",
Changelog = ChangelogOptions.Default with
{
Header = "Project1 header",
Path = "../docs"
}
},
new ProjectOptions
{
Name = "Project2",
Path = "project2",
Changelog = ChangelogOptions.Default with
{
Header = "Project2 header",
Path = "../docs"
}
}
};

var config = new FileConfig
{
SkipDirty = true,
Projects = projects
};
var json = JsonConvert.SerializeObject(config);
File.WriteAllText(Path.Join(_testSetup.WorkingDirectory, ".versionize"), json);
var cliConfig = CliConfig.Create(new CommandLineApplication());

Should.Throw<CommandLineExitException>(() => ConfigProvider.GetSelectedOptions(_testSetup.WorkingDirectory, cliConfig));
_testPlatformAbstractions.Messages[0].ShouldBe("Two or more projects have changelog paths pointing to the same location.");
}

public void Dispose()
{
_testSetup.Dispose();
}
}
23 changes: 23 additions & 0 deletions Versionize/Config/ConfigProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public static VersionizeOptions GetSelectedOptions(string cwd, CliConfig cliConf
fileConfig,
cliConfig);

ValidateChangelogPaths(fileConfig, options.WorkingDirectory);

CommandLineUI.Verbosity = MergeBool(cliConfig.Silent.HasValue(), fileConfig?.Silent)
? LogLevel.Silent
: LogLevel.All;
Expand Down Expand Up @@ -92,4 +94,25 @@ private static bool MergeBool(bool overridingValue, bool? optionalValue)
{
return overridingValue ? overridingValue : (optionalValue ?? false);
}

private static void ValidateChangelogPaths(FileConfig fileConfig, string cwd)
{
if (fileConfig?.Projects is null)
{
return;
}

var changelogPaths = new HashSet<string>();

foreach (var project in fileConfig.Projects)
{
var changelogPath = Path.Combine(cwd, project.Path, project.Changelog?.Path ?? string.Empty);
var fullChangelogPath = Path.GetFullPath(changelogPath);

if (!changelogPaths.Add(fullChangelogPath))
{
CommandLineUI.Exit("Two or more projects have changelog paths pointing to the same location.", 1);
}
}
}
}

0 comments on commit 4fb2ffa

Please # to comment.