Skip to content

Commit

Permalink
fix: repository path not being inferred
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardochaia committed Jul 10, 2021
1 parent 3e1a0fb commit 664ab50
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 43 deletions.
56 changes: 56 additions & 0 deletions src/dotnet-affected.Tests/RepositoryPathTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Affected.Cli.Commands;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace Affected.Cli.Tests
{
public class RepositoryPathTests
{
private class TestPathsClassData : IEnumerable<object[]>
{
public IEnumerator<object[]> GetEnumerator()
{
yield return new object[]
{
"/home/lchaia/dotnet-affected", "", "/home/lchaia/dotnet-affected"
};
yield return new object[]
{
"", "/home/lchaia/dotnet-affected/Affected.sln", "/home/lchaia/dotnet-affected"
};
yield return new object[]
{
"/home/lchaia/dotnet-affected", "/home/lchaia/dotnet-affected/subdirectory/other/Affected.sln",
"/home/lchaia/dotnet-affected"
};
yield return new object[]
{
"", "", Environment.CurrentDirectory
};
}

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

[Theory]
[ClassData(typeof(TestPathsClassData))]
public void Should_determine_repository_path_correctly(
string repositoryPath,
string solutionPath,
string expected)
{
var data = new CommandExecutionData(
repositoryPath,
solutionPath,
string.Empty,
string.Empty,
false,
Enumerable.Empty<string>());

Assert.Equal(expected, data.RepositoryPath);
}
}
}
6 changes: 4 additions & 2 deletions src/dotnet-affected/Commands/AffectedRootCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ public RepositoryPathOptions()
"--repository-path", "-p"
})
{
this.Description = "Path to the root of the repository, where the .git directory is.";
this.Description = "Path to the root of the repository, where the .git directory is.\n" +
"[Defaults to current directory, or solution's directory when using --solution-path]";
}
}

Expand All @@ -94,7 +95,8 @@ public SolutionPathOption()
})
{
this.Description =
"Path to a Solution file (.sln) used to find all projects that may be affected. When omitted, will search for project files inside --repository-path.";
"Path to a Solution file (.sln) used to discover projects that may be affected. " +
"When omitted, will search for project files inside --repository-path.";
}
}

Expand Down
36 changes: 4 additions & 32 deletions src/dotnet-affected/Commands/Context/CommandExecutionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.IO;
using System.IO;
using System.Linq;

namespace Affected.Cli.Commands
Expand All @@ -16,7 +15,6 @@ internal class CommandExecutionContext : ICommandExecutionContext
private readonly Lazy<IEnumerable<ProjectGraphNode>> _changedProjects;
private readonly Lazy<IEnumerable<ProjectGraphNode>> _affectedProjects;
private readonly Lazy<ProjectGraph> _graph;
private readonly Lazy<string> _repositoryPath;

public CommandExecutionContext(
CommandExecutionData executionData,
Expand All @@ -31,7 +29,6 @@ internal class CommandExecutionContext : ICommandExecutionContext
// For error handling to be managed properly at the handler level,
// we use Lazies so that its done on demand when its actually needed
// instead of happening here on the constructor
_repositoryPath = new Lazy<string>(DetermineRepositoryPath);
_graph = new Lazy<ProjectGraph>(BuildProjectGraph);
_changedProjects = new Lazy<IEnumerable<ProjectGraphNode>>(DetermineChangedProjects);
_affectedProjects = new Lazy<IEnumerable<ProjectGraphNode>>(
Expand Down Expand Up @@ -63,31 +60,6 @@ private ProjectGraph BuildProjectGraph()
return output;
}

private string DetermineRepositoryPath()
{
// the argument takes precedence.
if (!string.IsNullOrWhiteSpace(_executionData.RepositoryPath))
{
return _executionData.RepositoryPath;
}

// if no arguments, then use current directory
if (string.IsNullOrWhiteSpace(_executionData.SolutionPath))
{
return Environment.CurrentDirectory;
}

// When using solution, and no path specified, assume the solution's directory
var solutionDirectory = Path.GetDirectoryName(_executionData.SolutionPath);
if (string.IsNullOrWhiteSpace(solutionDirectory))
{
throw new InvalidOperationException(
$"Failed to determine directory from solution path {_executionData.SolutionPath}");
}

return solutionDirectory;
}

private IEnumerable<ProjectGraphNode> DetermineChangedProjects()
{
if (!_executionData.AssumeChanges.Any())
Expand All @@ -105,7 +77,7 @@ private IProjectDiscoverer BuildProjectDiscoverer()
{
if (string.IsNullOrWhiteSpace(_executionData.SolutionPath))
{
WriteLine($"Discovering projects from {_repositoryPath.Value}");
WriteLine($"Discovering projects from {_executionData.RepositoryPath}");
return new DirectoryProjectDiscoverer();
}

Expand All @@ -118,9 +90,9 @@ private IEnumerable<ProjectGraphNode> FindNodesThatChangedUsingChangesProvider()
// Get all files that have changed
var filesWithChanges = this._changesProvider
.GetChangedFiles(
_repositoryPath.Value,
this._executionData.From,
this._executionData.To)
_executionData.RepositoryPath,
_executionData.From,
_executionData.To)
.ToList();

// Match which files belong to which of our known projects
Expand Down
45 changes: 36 additions & 9 deletions src/dotnet-affected/Commands/Context/CommandExecutionData.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Affected.Cli.Commands
Expand All @@ -11,14 +13,14 @@ namespace Affected.Cli.Commands
internal class CommandExecutionData
{
public CommandExecutionData(
string repositoryPath,
string solutionPath,
string from,
string to,
bool verbose,
IEnumerable<string>? assumeChanges)
string repositoryPath,
string solutionPath,
string from,
string to,
bool verbose,
IEnumerable<string>? assumeChanges)
{
this.RepositoryPath = repositoryPath;
this.RepositoryPath = DetermineRepositoryPath(repositoryPath, solutionPath);
this.SolutionPath = solutionPath;
this.To = to;
this.From = from;
Expand All @@ -29,13 +31,38 @@ internal class CommandExecutionData
public string RepositoryPath { get; }

public string SolutionPath { get; }

public string From { get; }

public string To { get; }

public bool Verbose { get; }

public IEnumerable<string> AssumeChanges { get; }

private static string DetermineRepositoryPath(string repositoryPath, string solutionPath)
{
// the argument takes precedence.
if (!string.IsNullOrWhiteSpace(repositoryPath))
{
return repositoryPath;
}

// if no arguments, then use current directory
if (string.IsNullOrWhiteSpace(solutionPath))
{
return Environment.CurrentDirectory;
}

// When using solution, and no path specified, assume the solution's directory
var solutionDirectory = Path.GetDirectoryName(solutionPath);
if (string.IsNullOrWhiteSpace(solutionDirectory))
{
throw new InvalidOperationException(
$"Failed to determine directory from solution path {solutionPath}");
}

return solutionDirectory;
}
}
}

0 comments on commit 664ab50

Please sign in to comment.