Skip to content

Commit

Permalink
Use environment variables and non-local configurations conditionally (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tmat committed Jun 27, 2019
1 parent a048fc2 commit 5f6615b
Show file tree
Hide file tree
Showing 27 changed files with 446 additions and 98 deletions.
19 changes: 17 additions & 2 deletions src/Microsoft.Build.Tasks.Git.UnitTests/GitConfigTests.cs
Expand Up @@ -15,7 +15,7 @@ private static IEnumerable<string> Inspect(GitConfig config)
=> config.EnumerateVariables().Select(kvp => $"{kvp.Key}={string.Join("|", kvp.Value)}");

private static GitConfig LoadFromString(string gitDirectory, string configPath, string configContent)
=> new GitConfig.Reader(gitDirectory, gitDirectory, new GitEnvironment(Path.GetTempPath()), _ => new StringReader(configContent)).
=> new GitConfig.Reader(gitDirectory, gitDirectory, GitEnvironment.Empty, _ => new StringReader(configContent)).
LoadFrom(configPath);

[Fact]
Expand Down Expand Up @@ -160,6 +160,21 @@ TextReader openFile(string path)
}, Inspect(config));
}

[Fact]
public void ConditionalInclude_HomeNotSupported()
{
var gitDirectory = PathUtils.ToPosixPath(Path.Combine(Path.GetTempPath(), ".git")) + "/";

var config = @"
[includeIf ""gitdir/i:~/**/.GIT/""]
path = xyz
";

var reader = new GitConfig.Reader(gitDirectory, gitDirectory, GitEnvironment.Empty, _ => new StringReader(config));

Assert.Throws<NotSupportedException>(() => reader.LoadFrom(Path.Combine(gitDirectory, "config")));
}

[Fact]
public void IncludeRecursion()
{
Expand Down Expand Up @@ -189,7 +204,7 @@ TextReader openFile(string path)
});
}

Assert.Throws<InvalidDataException>(() => new GitConfig.Reader(gitDirectory, gitDirectory, new GitEnvironment("/home"), openFile).LoadFrom(Path.Combine(gitDirectory, "config")));
Assert.Throws<InvalidDataException>(() => new GitConfig.Reader(gitDirectory, gitDirectory, GitEnvironment.Empty, openFile).LoadFrom(Path.Combine(gitDirectory, "config")));
}

[Theory]
Expand Down
9 changes: 4 additions & 5 deletions src/Microsoft.Build.Tasks.Git.UnitTests/GitDataTests.cs
Expand Up @@ -16,7 +16,6 @@ public class GitDataTests
[Fact]
public void MinimalGitData()
{
var environment = new GitEnvironment("/home");
var repoDir = Temp.CreateDirectory();

var gitDir = repoDir.CreateDirectory(".git");
Expand All @@ -42,13 +41,13 @@ public void MinimalGitData()
gitDirSub.CreateDirectory("objects");
gitDirSub.CreateDirectory("refs");

var repository = GitRepository.OpenRepository(repoDir.Path, environment);
var repository = GitRepository.OpenRepository(repoDir.Path, GitEnvironment.Empty);

Assert.Equal("http://github.com/test-org/test-repo", GitOperations.GetRepositoryUrl(repository));
Assert.Equal("http://github.com/test-org/test-repo", GitOperations.GetRepositoryUrl(repository, remoteName: null));
Assert.Equal("1111111111111111111111111111111111111111", repository.GetHeadCommitSha());

var warnings = new List<(string, object[])>();
var sourceRoots = GitOperations.GetSourceRoots(repository, (message, args) => warnings.Add((message, args)));
var sourceRoots = GitOperations.GetSourceRoots(repository, remoteName: null, (message, args) => warnings.Add((message, args)));
AssertEx.Equal(new[]
{
$@"'{repoDir.Path}{s}' SourceControl='git' RevisionId='1111111111111111111111111111111111111111' ScmRepositoryUrl='http://github.com/test-org/test-repo'",
Expand All @@ -65,7 +64,7 @@ public void MinimalGitData()
new MockItem(@"sub\ignore_in_submodule_d"),
};

var untrackedFiles = GitOperations.GetUntrackedFiles(repository, files, repoDir.Path, path => GitRepository.OpenRepository(path, environment));
var untrackedFiles = GitOperations.GetUntrackedFiles(repository, files, repoDir.Path, path => GitRepository.OpenRepository(path, GitEnvironment.Empty));

AssertEx.Equal(new[]
{
Expand Down
42 changes: 20 additions & 22 deletions src/Microsoft.Build.Tasks.Git.UnitTests/GitOperationsTests.cs
Expand Up @@ -16,8 +16,6 @@ public class GitOperationsTests
private static readonly char s = Path.DirectorySeparatorChar;
private static readonly string s_root = (s == '/') ? "/usr/src" : @"C:\src";

private static readonly GitEnvironment s_environment = new GitEnvironment("/home");

private string _workingDir = s_root;

private GitRepository CreateRepository(
Expand All @@ -30,7 +28,7 @@ public class GitOperationsTests
workingDir ??= _workingDir;
var gitDir = Path.Combine(workingDir, ".git");
return new GitRepository(
s_environment,
GitEnvironment.Empty,
config ?? GitConfig.Empty,
gitDir,
gitDir,
Expand Down Expand Up @@ -78,7 +76,7 @@ public void GetRepositoryUrl_NoRemotes()
{
var repo = CreateRepository();
var warnings = new List<(string, object[])>();
Assert.Null(GitOperations.GetRepositoryUrl(repo, (message, args) => warnings.Add((message, args))));
Assert.Null(GitOperations.GetRepositoryUrl(repo, remoteName: null, logWarning: (message, args) => warnings.Add((message, args))));
AssertEx.Equal(new[] { Resources.RepositoryHasNoRemote }, warnings.Select(TestUtilities.InspectDiagnostic));
}

Expand All @@ -91,7 +89,7 @@ public void GetRepositoryUrl_Origin()

var warnings = new List<(string, object[])>();

Assert.Equal("http://github.com/origin", GitOperations.GetRepositoryUrl(repo, (message, args) => warnings.Add((message, args))));
Assert.Equal("http://github.com/origin", GitOperations.GetRepositoryUrl(repo, remoteName: null, logWarning: (message, args) => warnings.Add((message, args))));

Assert.Empty(warnings);
}
Expand All @@ -105,7 +103,7 @@ public void GetRepositoryUrl_NoOrigin()

var warnings = new List<(string, object[])>();

Assert.Equal("http://github.com/abc", GitOperations.GetRepositoryUrl(repo, (message, args) => warnings.Add((message, args))));
Assert.Equal("http://github.com/abc", GitOperations.GetRepositoryUrl(repo, remoteName: null, logWarning: (message, args) => warnings.Add((message, args))));

Assert.Empty(warnings);
}
Expand All @@ -120,8 +118,8 @@ public void GetRepositoryUrl_Specified()
var warnings = new List<(string, object[])>();

Assert.Equal("http://github.com/abc",
GitOperations.GetRepositoryUrl(repo, (message, args) => warnings.Add((message, args)),
remoteName: "abc"));
GitOperations.GetRepositoryUrl(repo, remoteName: "abc",
logWarning: (message, args) => warnings.Add((message, args))));

Assert.Empty(warnings);
}
Expand All @@ -136,8 +134,8 @@ public void GetRepositoryUrl_SpecifiedNotFound_OriginFallback()
var warnings = new List<(string, object[])>();

Assert.Equal("http://github.com/origin",
GitOperations.GetRepositoryUrl(repo, (message, args) => warnings.Add((message, args)),
remoteName: "myremote"));
GitOperations.GetRepositoryUrl(repo, remoteName: "myremote",
logWarning: (message, args) => warnings.Add((message, args))));

AssertEx.Equal(new[]
{
Expand All @@ -155,8 +153,8 @@ public void GetRepositoryUrl_SpecifiedNotFound_FirstFallback()
var warnings = new List<(string, object[])>();

Assert.Equal("http://github.com/abc",
GitOperations.GetRepositoryUrl(repo, (message, args) => warnings.Add((message, args)),
remoteName: "myremote"));
GitOperations.GetRepositoryUrl(repo, remoteName: "myremote",
logWarning: (message, args) => warnings.Add((message, args))));

AssertEx.Equal(new[]
{
Expand All @@ -170,7 +168,7 @@ public void GetRepositoryUrl_BadUrl()
var repo = CreateRepository(config: CreateConfig(("remote.origin.url", "http://?")));

var warnings = new List<(string, object[])>();
Assert.Null(GitOperations.GetRepositoryUrl(repo, (message, args) => warnings.Add((message, args))));
Assert.Null(GitOperations.GetRepositoryUrl(repo, remoteName: null, logWarning: (message, args) => warnings.Add((message, args))));
AssertEx.Equal(new[]
{
string.Format(Resources.InvalidRepositoryRemoteUrl, "origin", "http://?")
Expand All @@ -187,7 +185,7 @@ public void GetRepositoryUrl_InsteadOf()
})));

var warnings = new List<(string, object[])>();
Assert.Equal("ssh://git@github.com/org/repo", GitOperations.GetRepositoryUrl(repo, (message, args) => warnings.Add((message, args))));
Assert.Equal("ssh://git@github.com/org/repo", GitOperations.GetRepositoryUrl(repo, remoteName: null, logWarning: (message, args) => warnings.Add((message, args))));
Assert.Empty(warnings);
}

Expand Down Expand Up @@ -303,7 +301,7 @@ public void GetSourceRoots_RepoWithoutCommits()
var repo = CreateRepository();

var warnings = new List<(string, object[])>();
var items = GitOperations.GetSourceRoots(repo, (message, args) => warnings.Add((message, args)));
var items = GitOperations.GetSourceRoots(repo, remoteName: null, (message, args) => warnings.Add((message, args)));

Assert.Empty(items);
AssertEx.Equal(new[] { Resources.RepositoryHasNoCommit }, warnings.Select(TestUtilities.InspectDiagnostic));
Expand All @@ -320,7 +318,7 @@ public void GetSourceRoots_RepoWithoutCommitsWithSubmodules()
CreateSubmodule("1", "sub/2", "http://2.com", "2222222222222222222222222222222222222222"))); ;

var warnings = new List<(string, object[])>();
var items = GitOperations.GetSourceRoots(repo, (message, args) => warnings.Add((message, args)));
var items = GitOperations.GetSourceRoots(repo, remoteName: null, (message, args) => warnings.Add((message, args)));

AssertEx.Equal(new[]
{
Expand All @@ -341,7 +339,7 @@ public void GetSourceRoots_RepoWithCommitsWithSubmodules()
CreateSubmodule("1", "sub/2", "http://2.com", "2222222222222222222222222222222222222222")));

var warnings = new List<(string, object[])>();
var items = GitOperations.GetSourceRoots(repo, (message, args) => warnings.Add((message, args)));
var items = GitOperations.GetSourceRoots(repo, remoteName: null, (message, args) => warnings.Add((message, args)));

AssertEx.Equal(new[]
{
Expand All @@ -365,7 +363,7 @@ public void GetSourceRoots_RelativeSubmodulePaths_Windows()
CreateSubmodule("2", "sub/2", "../a", "2222222222222222222222222222222222222222")));

var warnings = new List<(string, object[])>();
var items = GitOperations.GetSourceRoots(repo, (message, args) => warnings.Add((message, args)));
var items = GitOperations.GetSourceRoots(repo, remoteName: null, (message, args) => warnings.Add((message, args)));

AssertEx.Equal(new[]
{
Expand All @@ -389,7 +387,7 @@ public void GetSourceRoots_RelativeSubmodulePaths_Windows_UnicodeAndEscapes()
CreateSubmodule("%25ለ", "sub/%25ለ", "../a", "2222222222222222222222222222222222222222")));

var warnings = new List<(string, object[])>();
var items = GitOperations.GetSourceRoots(repo, (message, args) => warnings.Add((message, args)));
var items = GitOperations.GetSourceRoots(repo, remoteName: null, (message, args) => warnings.Add((message, args)));

AssertEx.Equal(new[]
{
Expand All @@ -413,7 +411,7 @@ public void GetSourceRoots_RelativeSubmodulePaths_Unix()
CreateSubmodule("2", "sub/2", "../a", "2222222222222222222222222222222222222222")));

var warnings = new List<(string, object[])>();
var items = GitOperations.GetSourceRoots(repo, (message, args) => warnings.Add((message, args)));
var items = GitOperations.GetSourceRoots(repo, remoteName: null, (message, args) => warnings.Add((message, args)));

AssertEx.Equal(new[]
{
Expand All @@ -437,7 +435,7 @@ public void GetSourceRoots_RelativeSubmodulePaths_Unix_UnicodeAndEscapes()
CreateSubmodule("%25ለ", "sub/%25ለ", "../a", "2222222222222222222222222222222222222222")));

var warnings = new List<(string, object[])>();
var items = GitOperations.GetSourceRoots(repo, (message, args) => warnings.Add((message, args)));
var items = GitOperations.GetSourceRoots(repo, remoteName: null, (message, args) => warnings.Add((message, args)));

AssertEx.Equal(new[]
{
Expand All @@ -459,7 +457,7 @@ public void GetSourceRoots_InvalidSubmoduleUrl()
CreateSubmodule("3", "sub/3", "http://3.com", "3333333333333333333333333333333333333333")));

var warnings = new List<(string, object[])>();
var items = GitOperations.GetSourceRoots(repo, (message, args) => warnings.Add((message, args)));
var items = GitOperations.GetSourceRoots(repo, remoteName: null, (message, args) => warnings.Add((message, args)));

AssertEx.Equal(new[]
{
Expand Down
10 changes: 5 additions & 5 deletions src/Microsoft.Build.Tasks.Git.UnitTests/GitRepositoryTests.cs
Expand Up @@ -143,7 +143,7 @@ public void OpenRepository()
}

[Fact]
public void OpenReopsitory_VersionNotSupported()
public void OpenRepository_VersionNotSupported()
{
using var temp = new TempRoot();

Expand Down Expand Up @@ -190,7 +190,7 @@ public void Submodules()
[submodule ""S5""] # ignore if url unspecified
path = s4
");
var repository = new GitRepository(new GitEnvironment("/home"), GitConfig.Empty, gitDir.Path, gitDir.Path, workingDir.Path);
var repository = new GitRepository(GitEnvironment.Empty, GitConfig.Empty, gitDir.Path, gitDir.Path, workingDir.Path);

var submodules = GitRepository.EnumerateSubmoduleConfig(repository.ReadSubmoduleConfig());
AssertEx.Equal(new[]
Expand Down Expand Up @@ -261,7 +261,7 @@ public void Submodules_Errors()
path = sub10
url = http://github.com
");
var repository = new GitRepository(new GitEnvironment("/home"), GitConfig.Empty, gitDir.Path, gitDir.Path, workingDir.Path);
var repository = new GitRepository(GitEnvironment.Empty, GitConfig.Empty, gitDir.Path, gitDir.Path, workingDir.Path);

var submodules = repository.GetSubmodules();
AssertEx.Equal(new[]
Expand Down Expand Up @@ -348,7 +348,7 @@ public void GetHeadCommitSha()
var gitDir = temp.CreateDirectory();
gitDir.CreateFile("HEAD").WriteAllText("ref: refs/heads/master \t\v\r\n");

var repository = new GitRepository(new GitEnvironment("/home"), GitConfig.Empty, gitDir.Path, commonDir.Path, workingDirectory: null);
var repository = new GitRepository(GitEnvironment.Empty, GitConfig.Empty, gitDir.Path, commonDir.Path, workingDirectory: null);
Assert.Equal("0000000000000000000000000000000000000000", repository.GetHeadCommitSha());
}

Expand All @@ -369,7 +369,7 @@ public void GetSubmoduleHeadCommitSha()
submoduleRefsHeadsDir.CreateFile("master").WriteAllText("0000000000000000000000000000000000000000");
submoduleGitDir.CreateFile("HEAD").WriteAllText("ref: refs/heads/master");

var repository = new GitRepository(new GitEnvironment("/home"), GitConfig.Empty, gitDir.Path, gitDir.Path, workingDir.Path);
var repository = new GitRepository(GitEnvironment.Empty, GitConfig.Empty, gitDir.Path, gitDir.Path, workingDir.Path);
Assert.Equal("0000000000000000000000000000000000000000", repository.GetSubmoduleHeadCommitSha(submoduleWorkingDir.Path));
}
}
Expand Down

0 comments on commit 5f6615b

Please sign in to comment.