Skip to content

Commit

Permalink
Fixed naming conflicts in monorepos with single solution (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalSenn authored Aug 9, 2023
1 parent 6fb3ecd commit 1c052c8
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using Confix.Tool.Commands.Temp;
using Confix.Tool.Schema;
using static Confix.Tool.Abstractions.ProjectConfiguration;

Expand Down Expand Up @@ -131,12 +132,10 @@ public void WriteTo(Utf8JsonWriter writer)

public static ProjectDefinition From(ProjectConfiguration configuration)
{
var lastConfigurationFile =
configuration.SourceFiles.LastOrDefault(x => x.File.Name == FileNames.ConfixProject);
var lastConfigurationFile = configuration.SourceFiles
.LastOrDefault(x => x.File.Name == FileNames.ConfixProject);

var name = configuration.Name
?? lastConfigurationFile?.File.Directory?.Name
?? DefaultName;
var name = GetProjectName(configuration);

var environments =
configuration.Environments?.Select(EnvironmentDefinition.From).ToArray() ??
Expand Down Expand Up @@ -184,6 +183,40 @@ public static ProjectDefinition From(ProjectConfiguration configuration)
lastConfigurationFile?.File.Directory);
}

private static string GetProjectName(ProjectConfiguration configuration)
{
if (configuration.Name is not null)
{
return configuration.Name;
}

var lastProjectFile = configuration.SourceFiles
.LastOrDefault(x => x.File.Name == FileNames.ConfixProject)
?.File;

if (lastProjectFile is { Directory: { } lastProject })
{
var solution = lastProject.FindInTree(FileNames.ConfixSolution);

if (solution is not null &&
new FileInfo(solution) is { Directory: { } solutionDirectory })
{
var relative = Path
.GetRelativePath(solutionDirectory.FullName, lastProject.FullName)
.Replace(Path.DirectorySeparatorChar, '.');

if (!string.IsNullOrWhiteSpace(relative))
{
return relative;
}
}

return lastProjectFile.Directory?.Name ?? DefaultName;
}

return DefaultName;
}

public static ProjectDefinition Instance { get; } = new(
"root",
Array.Empty<EnvironmentDefinition>(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@ public sealed class JsonSchemaDefinition
public required string RelativePathToProject { get; set; }

public required IList<string> FileMatch { get; set; }

}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ public async Task Should_Discover_Component()
feature.Component,
feature.Project,
feature.Solution
}.ToJsonString().MatchSnapshot();
}.ToJsonString()
.MatchSnapshot();
}

[Fact]
Expand Down Expand Up @@ -130,6 +131,51 @@ public async Task Should_Discover_Project()
.MatchSnapshot();
}

[Fact]
public async Task Should_Discover_MonoRepo()
{
// arrange
await SetupHome();
await SetupConfixRoot();
await SetupMonoRepo();
await SetupProject();
await SetupComponent();
var middelwareContext = new Mock<IMiddlewareContext>(MockBehavior.Strict);
var featureCollection = new FeatureCollection();
var executionContext = new StubExecutionContext(_testProject, _testHome);
middelwareContext.SetupGet(x => x.CancellationToken).Returns(CancellationToken.None);
middelwareContext.SetupGet(x => x.Logger).Returns(ConsoleLogger.NullLogger);
middelwareContext.SetupGet(x => x.Status).Returns(Mock.Of<IStatus>());
middelwareContext.SetupGet(x => x.Execution).Returns(executionContext);
middelwareContext.SetupGet(x => x.Features).Returns(featureCollection);
var middleware = new LoadConfigurationMiddleware();
var isInvoked = false;
MiddlewareDelegate next = _ =>
{
isInvoked = true;
return Task.CompletedTask;
};

// act
await middleware.InvokeAsync(middelwareContext.Object, next);

// assert
Assert.True(isInvoked);
var feature = featureCollection.Get<ConfigurationFeature>();
Assert.NotNull(feature);
Assert.Equal(ConfigurationScope.Project, feature.Scope);
Assert.Null(feature.Component);
Assert.NotNull(feature.Project);
Assert.NotNull(feature.Solution);
new
{
feature.Component,
feature.Project,
feature.Solution
}.ToJsonString()
.MatchSnapshot();
}

[Fact]
public async Task Should_Discover_Solution()
{
Expand Down Expand Up @@ -224,78 +270,92 @@ private async Task SetupComponent()
{
await File.WriteAllTextAsync(_testComponentConfig,
"""
{
"name": "TestComponent",
"inputs": [
{
"type": "graphql",
"additional": "property"
},
{
"type": "dotnet",
{
"name": "TestComponent",
"inputs": [
{
"type": "graphql",
"additional": "property"
},
{
"type": "dotnet",
"additional2": "property"
}
],
"outputs": [{
"type": "schema",
"additional2": "property"
}
],
"outputs": [{
"type": "schema",
"additional2": "property"
}]
}
""");
}]
}
""");
}

private async Task SetupProject()
{
await File.WriteAllTextAsync(_testProjectConfig,
"""
{
"environments": [
"dev", "uat", "prod"
]
}
""");
{
"environments": [
"dev", "uat", "prod"
]
}
""");
}

private async Task SetupRepo()
{
await File.WriteAllTextAsync(_testRepoConfig,
"""
{
"project": {
"environments": [
"dev", "prod", { "name": "uat"}
]
}
}
""");
{
"project": {
"environments": [
"dev", "prod", { "name": "uat"}
]
}
}
""");
}

private async Task SetupMonoRepo()
{
await File.WriteAllTextAsync(Path.Combine(_monoRepo, FileNames.ConfixSolution),
"""
{
"project": {
"environments": [
"dev", "prod", { "name": "uat"}
]
}
}
""");
}

private async Task SetupConfixRoot()
{
await File.WriteAllTextAsync(_confixRoot,
"""
{
"isRoot":false,
"project": {
"environments": [
"dev", { "name": "prod"}
]
}
}
""");
{
"isRoot":false,
"project": {
"environments": [
"dev", { "name": "prod"}
]
}
}
""");
}

private async Task SetupHome()
{
await File.WriteAllTextAsync(_testHomeConfig,
"""
{
"project": {
"environments": [
{ "name": "dev"}
]
}
}
""");
{
"project": {
"environments": [
{ "name": "dev"}
]
}
}
""");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"Component": null,
"Project": {
"Name": "repo.project",
"Environments": [
{
"Name": "dev",
"Enabled": false
},
{
"Name": "uat",
"Enabled": false
},
{
"Name": "prod",
"Enabled": false
}
],
"Components": [],
"Repositories": [],
"VariableProviders": [],
"ComponentProviders": [],
"ConfigurationFiles": [],
"Subprojects": [],
"ProjectType": 0
},
"Solution": {
"Component": null,
"Project": {
"Name": "__Default",
"Environments": [
{
"Name": "dev",
"Enabled": false
},
{
"Name": "prod",
"Enabled": false
},
{
"Name": "uat",
"Enabled": false
}
],
"Components": [],
"Repositories": [],
"VariableProviders": [],
"ComponentProviders": [],
"ConfigurationFiles": [],
"Subprojects": [],
"ProjectType": 0
}
}
}

0 comments on commit 1c052c8

Please sign in to comment.