Skip to content

Commit 0d7a15f

Browse files
Add SLNX Tests
1 parent 1452391 commit 0d7a15f

File tree

4 files changed

+118
-1
lines changed

4 files changed

+118
-1
lines changed

src/dotnet-affected/Commands/AffectedGlobalOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ internal static class AffectedGlobalOptions
2828
{
2929
"--filter-file-path"
3030
},
31-
description: "Path to a filter file (.sln) used to discover projects that may be affected.\n" +
31+
description: "Path to a filter file (.sln, .slnx) used to discover projects that may be affected.\n" +
3232
"When omitted, will search for project files inside --repository-path.");
3333

3434
public static readonly Option<bool> VerboseOption = new(aliases: new[]
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using DotnetAffected.Testing.Utils;
2+
using System.IO;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Xunit;
6+
7+
namespace DotnetAffected.Core.Tests
8+
{
9+
/// <summary>
10+
/// Tests for detecting changed projects when using a SolutionFile to filter.
11+
/// This should cover all tests where filtering should be applied
12+
/// </summary>
13+
public class ChangeDetectionUsingXmlSolutionTests
14+
: BaseDotnetAffectedTest
15+
{
16+
private readonly string _solutionPath = "test-solution.slnx";
17+
18+
protected override AffectedOptions Options => new AffectedOptions(
19+
this.Repository.Path,
20+
Path.Combine(this.Repository.Path, this._solutionPath));
21+
22+
[Fact]
23+
public async Task When_project_inside_solution_has_changes_project_should_have_changed()
24+
{
25+
// Create a project
26+
var projectName = "InventoryManagement";
27+
var msBuildProject = this.Repository.CreateCsProject(projectName);
28+
29+
// Create a solution which includes the project
30+
await this.Repository.CreateXmlSolutionAsync(_solutionPath, msBuildProject.FullPath);
31+
32+
Assert.Single(AffectedSummary.ProjectsWithChangedFiles);
33+
Assert.Empty(AffectedSummary.AffectedProjects);
34+
35+
var projectInfo = AffectedSummary.ProjectsWithChangedFiles.Single();
36+
Assert.Equal(projectName, projectInfo.GetProjectName());
37+
Assert.Equal(msBuildProject.FullPath, projectInfo.GetFullPath());
38+
}
39+
40+
[Fact]
41+
public async Task When_project_inside_solution_should_ignore_changes_outside_solution()
42+
{
43+
// Create a project
44+
var projectName = "InventoryManagement";
45+
var msBuildProject = this.Repository.CreateCsProject(projectName);
46+
47+
// Create a solution which includes the project
48+
await this.Repository.CreateXmlSolutionAsync(_solutionPath, msBuildProject.FullPath);
49+
50+
// Create a project that is outside the solution
51+
var outsiderproject = "OutsiderProject";
52+
this.Repository.CreateCsProject(outsiderproject);
53+
54+
Assert.Single(AffectedSummary.ProjectsWithChangedFiles);
55+
Assert.Empty(AffectedSummary.AffectedProjects);
56+
57+
var projectInfo = AffectedSummary.ProjectsWithChangedFiles.Single();
58+
Assert.Equal(projectName, projectInfo.GetProjectName());
59+
Assert.Equal(msBuildProject.FullPath, projectInfo.GetFullPath());
60+
}
61+
62+
[Fact]
63+
public async Task When_project_outside_solution_has_changed_nothing_should_be_affected()
64+
{
65+
// Create a project
66+
var projectName = "InventoryManagement";
67+
var msBuildProject = this.Repository.CreateCsProject(projectName);
68+
69+
// Create a solution which includes the project
70+
await this.Repository.CreateXmlSolutionAsync(_solutionPath, msBuildProject.FullPath);
71+
72+
// Commit so there are no changes
73+
this.Repository.StageAndCommit();
74+
75+
// Create a project that is outside the solution
76+
var outsiderName = "OutsiderProject";
77+
this.Repository.CreateCsProject(outsiderName);
78+
79+
Assert.Empty(AffectedSummary.AffectedProjects);
80+
}
81+
}
82+
}

test/DotnetAffected.Testing.Utils/Repository/TemporaryRepositoryExtensions.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,20 @@ public static async Task CreateSolutionAsync(
149149
await File.WriteAllTextAsync(solutionPath, solutionContents);
150150
}
151151

152+
public static async Task CreateXmlSolutionAsync(
153+
this TemporaryRepository repo,
154+
string solutionName,
155+
params string[] projectPaths)
156+
{
157+
var i = 0;
158+
var solutionContents = new SolutionFileBuilder { Projects = projectPaths.ToDictionary(p => i++.ToString()) }
159+
.BuildXmlSolution();
160+
161+
var solutionPath = Path.Combine(repo.Path, solutionName);
162+
163+
await File.WriteAllTextAsync(solutionPath, solutionContents);
164+
}
165+
152166
public static Task CreateTraversalProjectAsync(
153167
this TemporaryRepository repo,
154168
string traversalProjectPath,

test/DotnetAffected.Testing.Utils/SolutionFileBuilder.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,26 @@ public string BuildSolution()
9797

9898
return sb.ToString();
9999
}
100+
101+
public string BuildXmlSolution()
102+
{
103+
var sb = new StringBuilder();
104+
sb.AppendLine(@"<Solution>");
105+
sb.AppendLine(@" <Configurations>");
106+
sb.AppendLine(@" <Platform Name=""Any CPU"" />");
107+
sb.AppendLine(@" <Platform Name=""x64"" />");
108+
sb.AppendLine(@" <Platform Name=""x86"" />");
109+
sb.AppendLine(@" </Configurations>");
110+
111+
foreach (var project in Projects.Values)
112+
{
113+
sb.AppendLine(@$" <Project Path=""{project}"" />");
114+
115+
}
116+
117+
sb.AppendLine(@"</Solution>");
118+
119+
return sb.ToString();
120+
}
100121
}
101122
}

0 commit comments

Comments
 (0)