From fac5c10aef08c8742a403f362b11708513bb2fde Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Wed, 4 Dec 2019 10:57:01 -0500 Subject: [PATCH 01/26] Adding a very minimal implementation --- .../BuildServers/GitHubActions.cs | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/GitVersionCore/BuildServers/GitHubActions.cs diff --git a/src/GitVersionCore/BuildServers/GitHubActions.cs b/src/GitVersionCore/BuildServers/GitHubActions.cs new file mode 100644 index 0000000000..8b45bc2f6e --- /dev/null +++ b/src/GitVersionCore/BuildServers/GitHubActions.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace GitVersion.BuildServers +{ + using GitVersion.OutputVariables; + + public class GitHubActions: IBuildServer + { + public bool CanApplyToCurrentContext() + { + throw new NotImplementedException(); + } + + public string GenerateSetVersionMessage(VersionVariables variables) + { + throw new NotImplementedException(); + } + + public string[] GenerateSetParameterMessage(string name, string value) + { + throw new NotImplementedException(); + } + + public void WriteIntegration(Action writer, VersionVariables variables) + { + throw new NotImplementedException(); + } + + public string GetCurrentBranch(bool usingDynamicRepos) + { + throw new NotImplementedException(); + } + + public bool PreventFetch() + { + throw new NotImplementedException(); + } + + public bool ShouldCleanUpRemotes() + { + throw new NotImplementedException(); + } + } +} From 22849a4b7da860653978c3990b1ff94e563916bc Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Wed, 4 Dec 2019 11:24:54 -0500 Subject: [PATCH 02/26] Implementing a GitHubActions build server --- .../BuildServers/GitHubActionsTests.cs | 103 ++++++++++++++++++ .../BuildServers/GitHubActions.cs | 59 ++++++---- 2 files changed, 138 insertions(+), 24 deletions(-) create mode 100644 src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs diff --git a/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs b/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs new file mode 100644 index 0000000000..469da253b6 --- /dev/null +++ b/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs @@ -0,0 +1,103 @@ + +using System; +using GitVersion; +using GitVersion.BuildServers; +using GitVersion.Logging; +using NUnit.Framework; +using Shouldly; + +namespace GitVersionCore.Tests.BuildServers +{ + [TestFixture] + public class GitHubActionsTests : TestBase + { + private IEnvironment environment; + private ILog log; + + [SetUp] + public void SetUp() + { + log = new NullLog(); + environment = new TestEnvironment(); + environment.SetEnvironmentVariable("GITHUB_ACTION", Guid.NewGuid().ToString()); + } + + [TearDown] + public void TearDown() + { + environment.SetEnvironmentVariable("GITHUB_ACTION", null); + } + + [Test] + public void CanApplyToCurrentContextShouldBeTrueWhenEnvironmentVariableIsSet() + { + // Arrange + var buildServer = new GitHubActions(environment, log); + + // Act + var result = buildServer.CanApplyToCurrentContext(); + + // Assert + result.ShouldBeTrue(); + } + + [Test] + public void CanApplyToCurrentContextShouldBeFalseWhenEnvironmentVariableIsNotSet() + { + // Arrange + environment.SetEnvironmentVariable("GITHUB_ACTION", ""); + var buildServer = new GitHubActions(environment, log); + + // Act + var result = buildServer.CanApplyToCurrentContext(); + + // Assert + result.ShouldBeFalse(); + } + + [Test] + public void GetCurrentBranchShouldGetBranchIfSet() + { + // Arrange + const string expected = "actionsBranch"; + + environment.SetEnvironmentVariable("GITHUB_REF", $"refs/heads/{expected}"); + + var buildServer = new GitHubActions(environment, log); + + // Act + var result = buildServer.GetCurrentBranch(false); + + // Assert + result.ShouldBe(expected); + } + + [TestCase("GitVersion_Something", "1.0.0", "::set-output name=GitVersion_Something::1.0.0")] + public void GetSetParameterMessage(string key, string value, string expected) + { + // Arrange + var buildServer = new GitHubActions(environment, log); + + // Act + var result = buildServer.GenerateSetParameterMessage(key, value); + + // Assert + result.ShouldContain(s => true, 1); + result.ShouldBeEquivalentTo(new[] { expected }); + } + + [Test] + public void GetEmptyGenerateSetVersionMessage() + { + // Arrange + var buildServer = new GitHubActions(environment, log); + var vars = new TestableVersionVariables("1.0.0"); + + // Act + var message = buildServer.GenerateSetVersionMessage(vars); + + // Assert + message.ShouldBeEmpty(); + } + } +} diff --git a/src/GitVersionCore/BuildServers/GitHubActions.cs b/src/GitVersionCore/BuildServers/GitHubActions.cs index 8b45bc2f6e..7bc2f1014b 100644 --- a/src/GitVersionCore/BuildServers/GitHubActions.cs +++ b/src/GitVersionCore/BuildServers/GitHubActions.cs @@ -1,46 +1,57 @@ -using System; -using System.Collections.Generic; -using System.Text; +using GitVersion.Logging; +using GitVersion.OutputVariables; namespace GitVersion.BuildServers { - using GitVersion.OutputVariables; - - public class GitHubActions: IBuildServer + public class GitHubActions: BuildServerBase { - public bool CanApplyToCurrentContext() - { - throw new NotImplementedException(); - } + // https://help.github.com/en/actions/automating-your-workflow-with-github-actions/using-environment-variables#default-environment-variables - public string GenerateSetVersionMessage(VersionVariables variables) + public GitHubActions(IEnvironment environment, ILog log) : base(environment, log) { - throw new NotImplementedException(); } - public string[] GenerateSetParameterMessage(string name, string value) + public const string EnvironmentVariableName = "GITHUB_ACTION"; + protected override string EnvironmentVariable { get; } = EnvironmentVariableName; + public override bool CanApplyToCurrentContext() { - throw new NotImplementedException(); + return !string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(EnvironmentVariable)); } - public void WriteIntegration(Action writer, VersionVariables variables) + public override string GenerateSetVersionMessage(VersionVariables variables) { - throw new NotImplementedException(); - } + // There is no equivalent function in GitHub Actions. - public string GetCurrentBranch(bool usingDynamicRepos) - { - throw new NotImplementedException(); + return string.Empty; } - public bool PreventFetch() + public override string[] GenerateSetParameterMessage(string name, string value) { - throw new NotImplementedException(); + // https://help.github.com/en/actions/automating-your-workflow-with-github-actions/development-tools-for-github-actions#set-an-environment-variable-set-env + // Example + // echo "::set-output name=action_fruit::strawberry" + + return new [] + { + $"::set-output name={name}::{value}" + }; } - public bool ShouldCleanUpRemotes() + public override string GetCurrentBranch(bool usingDynamicRepos) { - throw new NotImplementedException(); + // GITHUB_REF + // The branch or tag ref that triggered the workflow. + // For example, refs/heads/feature-branch-1. If neither a branch or + // tag is available for the event type, the variable will not exist. + + var value = Environment.GetEnvironmentVariable("GITHUB_REF"); + if (!string.IsNullOrWhiteSpace(value)) + { + value = value.Substring("refs/heads/".Length); + return value; + } + + return base.GetCurrentBranch(usingDynamicRepos); } } } From a97d4890fee7ffefd9b8c7c7191d35bc728856d5 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Wed, 4 Dec 2019 16:46:44 -0500 Subject: [PATCH 03/26] Registring GitHubAction build server with DI --- src/GitVersionCore/GitVersionCoreModule.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/GitVersionCore/GitVersionCoreModule.cs b/src/GitVersionCore/GitVersionCoreModule.cs index 22a67d8031..49a3b60866 100644 --- a/src/GitVersionCore/GitVersionCoreModule.cs +++ b/src/GitVersionCore/GitVersionCoreModule.cs @@ -57,6 +57,7 @@ private static void RegisterBuildServers(IServiceCollection services) services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); + services.AddSingleton(); } private static void RegisterVersionStrategies(IServiceCollection services) From 34dbec24dfc574b7735b1c80aefe7d6f3feacb30 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Thu, 5 Dec 2019 00:17:11 -0500 Subject: [PATCH 04/26] Allowing GitVersionTask to set the environment variables of GitHub Actions --- src/GitVersionCore/BuildServers/GitHubActions.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/GitVersionCore/BuildServers/GitHubActions.cs b/src/GitVersionCore/BuildServers/GitHubActions.cs index 7bc2f1014b..0236ba4a85 100644 --- a/src/GitVersionCore/BuildServers/GitHubActions.cs +++ b/src/GitVersionCore/BuildServers/GitHubActions.cs @@ -3,6 +3,9 @@ namespace GitVersion.BuildServers { + using System; + using System.Linq; + public class GitHubActions: BuildServerBase { // https://help.github.com/en/actions/automating-your-workflow-with-github-actions/using-environment-variables#default-environment-variables @@ -53,5 +56,18 @@ public override string GetCurrentBranch(bool usingDynamicRepos) return base.GetCurrentBranch(usingDynamicRepos); } + + public override void WriteIntegration(Action writer, VersionVariables variables) + { + base.WriteIntegration(writer, variables); + + var strings = variables + .SelectMany(pair => GenerateSetParameterMessage(pair.Key, pair.Value)); + + foreach (var item in strings) + { + writer(item); + } + } } } From 3dc956697ec85692e560d422791460fde5be0e70 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Thu, 5 Dec 2019 00:18:35 -0500 Subject: [PATCH 05/26] Avoiding tags --- src/GitVersionCore/BuildServers/GitHubActions.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/GitVersionCore/BuildServers/GitHubActions.cs b/src/GitVersionCore/BuildServers/GitHubActions.cs index 0236ba4a85..9472dedb89 100644 --- a/src/GitVersionCore/BuildServers/GitHubActions.cs +++ b/src/GitVersionCore/BuildServers/GitHubActions.cs @@ -50,8 +50,13 @@ public override string GetCurrentBranch(bool usingDynamicRepos) var value = Environment.GetEnvironmentVariable("GITHUB_REF"); if (!string.IsNullOrWhiteSpace(value)) { - value = value.Substring("refs/heads/".Length); - return value; + const string refsHeads = "refs/heads/"; + + if (value.StartsWith(refsHeads)) + { + value = value.Substring(refsHeads.Length); + return value; + } } return base.GetCurrentBranch(usingDynamicRepos); From f0a32e5189c9c261f21d136f53568ee1196c2ef8 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Thu, 5 Dec 2019 00:37:39 -0500 Subject: [PATCH 06/26] Some fixes and some tests --- .../BuildServers/GitHubActionsTests.cs | 59 ++++++++++++++++++- .../BuildServers/GitHubActions.cs | 26 +++----- 2 files changed, 67 insertions(+), 18 deletions(-) diff --git a/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs b/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs index 469da253b6..5f73b46d4f 100644 --- a/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs +++ b/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs @@ -8,6 +8,9 @@ namespace GitVersionCore.Tests.BuildServers { + using System.Collections.Generic; + using Environment = System.Environment; + [TestFixture] public class GitHubActionsTests : TestBase { @@ -72,7 +75,22 @@ public void GetCurrentBranchShouldGetBranchIfSet() result.ShouldBe(expected); } - [TestCase("GitVersion_Something", "1.0.0", "::set-output name=GitVersion_Something::1.0.0")] + [Test] + public void GetCurrentBranchShouldNotMatchTag() + { + // Arrange + environment.SetEnvironmentVariable("GITHUB_REF", $"refs/tags/v1.0.0"); + + var buildServer = new GitHubActions(environment, log); + + // Act + var result = buildServer.GetCurrentBranch(false); + + // Assert + result.ShouldBeNull(); + } + + [TestCase("Something", "1.0.0", "::set-env name=GitVersion_Something::1.0.0")] public void GetSetParameterMessage(string key, string value, string expected) { // Arrange @@ -86,6 +104,45 @@ public void GetSetParameterMessage(string key, string value, string expected) result.ShouldBeEquivalentTo(new[] { expected }); } + [Test] + public void SkipEmptySetParameterMessage() + { + // Arrange + var buildServer = new GitHubActions(environment, log); + + // Act + var result = buildServer.GenerateSetParameterMessage("Hello", string.Empty); + + // Assert + result.ShouldBeEquivalentTo(new string[0]); + } + + [Test] + public void ShouldWriteIntegration() + { + // Arrange + var buildServer = new GitHubActions(environment, log); + + var vars = new TestableVersionVariables("1.0.0"); + + var list = new List(); + + // Act + buildServer.WriteIntegration(s => { list.Add(s); }, vars); + + // Assert + var expected = new List + { + "Executing GenerateSetVersionMessage for 'GitHubActions'.", + "", + "Executing GenerateBuildLogOutput for 'GitHubActions'.", + "::set-env name=GitVersion_Major::1.0.0" + }; + + string.Join(Environment.NewLine, list) + .ShouldBe(string.Join(Environment.NewLine, expected)); + } + [Test] public void GetEmptyGenerateSetVersionMessage() { diff --git a/src/GitVersionCore/BuildServers/GitHubActions.cs b/src/GitVersionCore/BuildServers/GitHubActions.cs index 9472dedb89..d3cbac2972 100644 --- a/src/GitVersionCore/BuildServers/GitHubActions.cs +++ b/src/GitVersionCore/BuildServers/GitHubActions.cs @@ -32,12 +32,17 @@ public override string[] GenerateSetParameterMessage(string name, string value) { // https://help.github.com/en/actions/automating-your-workflow-with-github-actions/development-tools-for-github-actions#set-an-environment-variable-set-env // Example - // echo "::set-output name=action_fruit::strawberry" + // echo "::set-env name=action_state::yellow" - return new [] + if (!string.IsNullOrWhiteSpace(value)) { - $"::set-output name={name}::{value}" - }; + return new[] + { + $"::set-env name=GitVersion_{name}::{value}" + }; + } + + return new string[0]; } public override string GetCurrentBranch(bool usingDynamicRepos) @@ -61,18 +66,5 @@ public override string GetCurrentBranch(bool usingDynamicRepos) return base.GetCurrentBranch(usingDynamicRepos); } - - public override void WriteIntegration(Action writer, VersionVariables variables) - { - base.WriteIntegration(writer, variables); - - var strings = variables - .SelectMany(pair => GenerateSetParameterMessage(pair.Key, pair.Value)); - - foreach (var item in strings) - { - writer(item); - } - } } } From a489aa8b081ed7af7c0eaa0d0380a143ff4f080f Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Thu, 5 Dec 2019 08:46:25 -0500 Subject: [PATCH 07/26] Adding debug information --- src/GitVersionCore/Common/BuildServerBase.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/GitVersionCore/Common/BuildServerBase.cs b/src/GitVersionCore/Common/BuildServerBase.cs index bc0de6bba8..4ac02235a8 100644 --- a/src/GitVersionCore/Common/BuildServerBase.cs +++ b/src/GitVersionCore/Common/BuildServerBase.cs @@ -5,6 +5,8 @@ namespace GitVersion { + using System.Linq; + public abstract class BuildServerBase : IBuildServer { protected readonly ILog Log; @@ -42,7 +44,7 @@ public virtual void WriteIntegration(Action writer, VersionVariables var writer($"Executing GenerateSetVersionMessage for '{GetType().Name}'."); writer(GenerateSetVersionMessage(variables)); - writer($"Executing GenerateBuildLogOutput for '{GetType().Name}'."); + writer($"Executing GenerateBuildLogOutput for '{GetType().Name}' Keys:{string.Join(";", variables.Select(pair => pair.Key))}."); foreach (var buildParameter in BuildOutputFormatter.GenerateBuildLogOutput(this, variables)) { writer(buildParameter); From 08f86bed6055425b502e4d8f2d8cb5926843c14d Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Thu, 5 Dec 2019 08:53:22 -0500 Subject: [PATCH 08/26] Fixing test --- src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs | 2 +- src/GitVersionCore/Common/BuildServerBase.cs | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs b/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs index 5f73b46d4f..639b521897 100644 --- a/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs +++ b/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs @@ -135,7 +135,7 @@ public void ShouldWriteIntegration() { "Executing GenerateSetVersionMessage for 'GitHubActions'.", "", - "Executing GenerateBuildLogOutput for 'GitHubActions'.", + "Executing GenerateBuildLogOutput for 'GitHubActions' Keys:Major.", "::set-env name=GitVersion_Major::1.0.0" }; diff --git a/src/GitVersionCore/Common/BuildServerBase.cs b/src/GitVersionCore/Common/BuildServerBase.cs index 4ac02235a8..9611ffef21 100644 --- a/src/GitVersionCore/Common/BuildServerBase.cs +++ b/src/GitVersionCore/Common/BuildServerBase.cs @@ -42,9 +42,11 @@ public virtual void WriteIntegration(Action writer, VersionVariables var return; } + var keys = string.Join(";", variables.Where(pair => !string.IsNullOrWhiteSpace(pair.Value)).Select(pair => pair.Key)); + writer($"Executing GenerateSetVersionMessage for '{GetType().Name}'."); writer(GenerateSetVersionMessage(variables)); - writer($"Executing GenerateBuildLogOutput for '{GetType().Name}' Keys:{string.Join(";", variables.Select(pair => pair.Key))}."); + writer($"Executing GenerateBuildLogOutput for '{GetType().Name}' Keys:{keys}."); foreach (var buildParameter in BuildOutputFormatter.GenerateBuildLogOutput(this, variables)) { writer(buildParameter); From 53deb6954156e2442ef621c5cf3a41efec335696 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Thu, 5 Dec 2019 09:14:49 -0500 Subject: [PATCH 09/26] Adding keys --- src/GitVersionTask/GitVersionTaskExecutor.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/GitVersionTask/GitVersionTaskExecutor.cs b/src/GitVersionTask/GitVersionTaskExecutor.cs index 9e24f94fd6..8f57b471da 100644 --- a/src/GitVersionTask/GitVersionTaskExecutor.cs +++ b/src/GitVersionTask/GitVersionTaskExecutor.cs @@ -8,6 +8,8 @@ namespace GitVersion.MSBuildTask { + using System.Linq; + public class GitVersionTaskExecutor : IGitVersionTaskExecutor { private readonly IFileSystem fileSystem; @@ -66,9 +68,11 @@ public void WriteVersionInfoToBuildLog(WriteVersionInfoToBuildLog task) var buildServer = buildServerResolver.Resolve(); if (buildServer != null) { + var keys = string.Join(";", versionVariables.Where(pair => !string.IsNullOrWhiteSpace(pair.Value)).Select(pair => pair.Key)); + logger.LogMessage($"Executing GenerateSetVersionMessage for '{buildServer.GetType().Name}'."); logger.LogMessage(buildServer.GenerateSetVersionMessage(versionVariables)); - logger.LogMessage($"Executing GenerateBuildLogOutput for '{buildServer.GetType().Name}'."); + logger.LogMessage($"Executing GenerateBuildLogOutput for '{GetType().Name}' Keys:{keys}."); foreach (var buildParameter in BuildOutputFormatter.GenerateBuildLogOutput(buildServer, versionVariables)) { logger.LogMessage(buildParameter); From fb4ed56ae7280fd4a2cde8c93e1109eb984fc8ae Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Thu, 5 Dec 2019 09:32:42 -0500 Subject: [PATCH 10/26] Debugging more --- src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs | 3 ++- src/GitVersionCore/BuildServers/GitHubActions.cs | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs b/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs index 639b521897..d6e7b1e223 100644 --- a/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs +++ b/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs @@ -90,7 +90,7 @@ public void GetCurrentBranchShouldNotMatchTag() result.ShouldBeNull(); } - [TestCase("Something", "1.0.0", "::set-env name=GitVersion_Something::1.0.0")] + [TestCase("Something", "1.0.0", "::set-env name=GitVersion_Something::1.0.0", Ignore = "Skip")] public void GetSetParameterMessage(string key, string value, string expected) { // Arrange @@ -118,6 +118,7 @@ public void SkipEmptySetParameterMessage() } [Test] + [Ignore("Skip")] public void ShouldWriteIntegration() { // Arrange diff --git a/src/GitVersionCore/BuildServers/GitHubActions.cs b/src/GitVersionCore/BuildServers/GitHubActions.cs index d3cbac2972..e04167513b 100644 --- a/src/GitVersionCore/BuildServers/GitHubActions.cs +++ b/src/GitVersionCore/BuildServers/GitHubActions.cs @@ -38,7 +38,8 @@ public override string[] GenerateSetParameterMessage(string name, string value) { return new[] { - $"::set-env name=GitVersion_{name}::{value}" + $"::set-env name=GitVersion_{name}::{value}", + $"set-env name=GitVersion_{name}::{value}", }; } From a154ed75f1de88deaa396497aa6649967b7b3317 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Thu, 5 Dec 2019 09:38:07 -0500 Subject: [PATCH 11/26] Changing variable writing method --- src/GitVersionCore/BuildServers/GitHubActions.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/GitVersionCore/BuildServers/GitHubActions.cs b/src/GitVersionCore/BuildServers/GitHubActions.cs index e04167513b..07a0b471e2 100644 --- a/src/GitVersionCore/BuildServers/GitHubActions.cs +++ b/src/GitVersionCore/BuildServers/GitHubActions.cs @@ -36,10 +36,11 @@ public override string[] GenerateSetParameterMessage(string name, string value) if (!string.IsNullOrWhiteSpace(value)) { + Console.WriteLine($"::set-env name=GitVersion_{name}::{value}"); + return new[] { - $"::set-env name=GitVersion_{name}::{value}", - $"set-env name=GitVersion_{name}::{value}", + $"Adding Environment Variable. name='GitVersion_{name}' value='{value}']" }; } From a316087c2066ea0d60053d9e0dcadf38ef161e1a Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Thu, 5 Dec 2019 09:56:31 -0500 Subject: [PATCH 12/26] Assuming I have the right idea --- .../BuildServers/GitHubActionsTests.cs | 48 ++++++++++++------- .../BuildServers/GitHubActions.cs | 12 ++--- 2 files changed, 38 insertions(+), 22 deletions(-) diff --git a/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs b/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs index d6e7b1e223..28c09a38c8 100644 --- a/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs +++ b/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs @@ -1,26 +1,37 @@ - using System; using GitVersion; using GitVersion.BuildServers; using GitVersion.Logging; using NUnit.Framework; using Shouldly; +using System.Collections.Generic; +using GitVersion.Configuration; +using NSubstitute; +using Environment = System.Environment; namespace GitVersionCore.Tests.BuildServers { - using System.Collections.Generic; - using Environment = System.Environment; [TestFixture] public class GitHubActionsTests : TestBase { + private IConsole console; private IEnvironment environment; private ILog log; + private List consoleLinesWritten; [SetUp] public void SetUp() { log = new NullLog(); + console = Substitute.For(); + + consoleLinesWritten = new List(); + console.WhenForAnyArgs(c => c.WriteLine(default)) + .Do(info => consoleLinesWritten.Add(info.Arg())); + console.WhenForAnyArgs(c => c.WriteLine()) + .Do(info => consoleLinesWritten.Add(string.Empty)); + environment = new TestEnvironment(); environment.SetEnvironmentVariable("GITHUB_ACTION", Guid.NewGuid().ToString()); } @@ -35,7 +46,7 @@ public void TearDown() public void CanApplyToCurrentContextShouldBeTrueWhenEnvironmentVariableIsSet() { // Arrange - var buildServer = new GitHubActions(environment, log); + var buildServer = new GitHubActions(environment, log, console); // Act var result = buildServer.CanApplyToCurrentContext(); @@ -49,7 +60,7 @@ public void CanApplyToCurrentContextShouldBeFalseWhenEnvironmentVariableIsNotSet { // Arrange environment.SetEnvironmentVariable("GITHUB_ACTION", ""); - var buildServer = new GitHubActions(environment, log); + var buildServer = new GitHubActions(environment, log, console); // Act var result = buildServer.CanApplyToCurrentContext(); @@ -66,7 +77,7 @@ public void GetCurrentBranchShouldGetBranchIfSet() environment.SetEnvironmentVariable("GITHUB_REF", $"refs/heads/{expected}"); - var buildServer = new GitHubActions(environment, log); + var buildServer = new GitHubActions(environment, log, console); // Act var result = buildServer.GetCurrentBranch(false); @@ -81,7 +92,7 @@ public void GetCurrentBranchShouldNotMatchTag() // Arrange environment.SetEnvironmentVariable("GITHUB_REF", $"refs/tags/v1.0.0"); - var buildServer = new GitHubActions(environment, log); + var buildServer = new GitHubActions(environment, log, console); // Act var result = buildServer.GetCurrentBranch(false); @@ -90,25 +101,29 @@ public void GetCurrentBranchShouldNotMatchTag() result.ShouldBeNull(); } - [TestCase("Something", "1.0.0", "::set-env name=GitVersion_Something::1.0.0", Ignore = "Skip")] - public void GetSetParameterMessage(string key, string value, string expected) + [TestCase("Something", "1.0.0", + "Adding Environment Variable. name='GitVersion_Something' value='1.0.0'", + "::set-env name=GitVersion_Something::1.0.0")] + public void GetSetParameterMessage(string key, string value, string expectedResult, string expectedConsole) { // Arrange - var buildServer = new GitHubActions(environment, log); + var buildServer = new GitHubActions(environment, log, console); // Act var result = buildServer.GenerateSetParameterMessage(key, value); // Assert result.ShouldContain(s => true, 1); - result.ShouldBeEquivalentTo(new[] { expected }); + result.ShouldBeEquivalentTo(new[] { expectedResult }); + + consoleLinesWritten.ShouldBeEquivalentTo(new List { expectedConsole }); } [Test] public void SkipEmptySetParameterMessage() { // Arrange - var buildServer = new GitHubActions(environment, log); + var buildServer = new GitHubActions(environment, log, console); // Act var result = buildServer.GenerateSetParameterMessage("Hello", string.Empty); @@ -118,11 +133,10 @@ public void SkipEmptySetParameterMessage() } [Test] - [Ignore("Skip")] public void ShouldWriteIntegration() { // Arrange - var buildServer = new GitHubActions(environment, log); + var buildServer = new GitHubActions(environment, log, console); var vars = new TestableVersionVariables("1.0.0"); @@ -137,18 +151,20 @@ public void ShouldWriteIntegration() "Executing GenerateSetVersionMessage for 'GitHubActions'.", "", "Executing GenerateBuildLogOutput for 'GitHubActions' Keys:Major.", - "::set-env name=GitVersion_Major::1.0.0" + "Adding Environment Variable. name='GitVersion_Major' value='1.0.0'" }; string.Join(Environment.NewLine, list) .ShouldBe(string.Join(Environment.NewLine, expected)); + + consoleLinesWritten.ShouldBeEquivalentTo(new List { "::set-env name=GitVersion_Major::1.0.0" }); } [Test] public void GetEmptyGenerateSetVersionMessage() { // Arrange - var buildServer = new GitHubActions(environment, log); + var buildServer = new GitHubActions(environment, log, console); var vars = new TestableVersionVariables("1.0.0"); // Act diff --git a/src/GitVersionCore/BuildServers/GitHubActions.cs b/src/GitVersionCore/BuildServers/GitHubActions.cs index 07a0b471e2..bdc3eb4004 100644 --- a/src/GitVersionCore/BuildServers/GitHubActions.cs +++ b/src/GitVersionCore/BuildServers/GitHubActions.cs @@ -1,17 +1,17 @@ using GitVersion.Logging; using GitVersion.OutputVariables; +using GitVersion.Configuration; namespace GitVersion.BuildServers { - using System; - using System.Linq; - public class GitHubActions: BuildServerBase { + private readonly IConsole console; // https://help.github.com/en/actions/automating-your-workflow-with-github-actions/using-environment-variables#default-environment-variables - public GitHubActions(IEnvironment environment, ILog log) : base(environment, log) + public GitHubActions(IEnvironment environment, ILog log, IConsole console) : base(environment, log) { + this.console = console; } public const string EnvironmentVariableName = "GITHUB_ACTION"; @@ -36,11 +36,11 @@ public override string[] GenerateSetParameterMessage(string name, string value) if (!string.IsNullOrWhiteSpace(value)) { - Console.WriteLine($"::set-env name=GitVersion_{name}::{value}"); + console.WriteLine($"::set-env name=GitVersion_{name}::{value}"); return new[] { - $"Adding Environment Variable. name='GitVersion_{name}' value='{value}']" + $"Adding Environment Variable. name='GitVersion_{name}' value='{value}'" }; } From b578b4c03b5586494ffe3d66b5cb975e90cc932c Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Thu, 5 Dec 2019 10:06:01 -0500 Subject: [PATCH 13/26] Also write to the current environment --- .../BuildServers/GitHubActionsTests.cs | 11 +++++++++++ src/GitVersionCore/BuildServers/GitHubActions.cs | 7 +++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs b/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs index 28c09a38c8..bf88bb9703 100644 --- a/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs +++ b/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs @@ -40,6 +40,7 @@ public void SetUp() public void TearDown() { environment.SetEnvironmentVariable("GITHUB_ACTION", null); + environment.SetEnvironmentVariable("GitVersion_Major", null); } [Test] @@ -109,6 +110,9 @@ public void GetSetParameterMessage(string key, string value, string expectedResu // Arrange var buildServer = new GitHubActions(environment, log, console); + // Assert + environment.GetEnvironmentVariable("GitVersion_Something").ShouldBeNullOrWhiteSpace(); + // Act var result = buildServer.GenerateSetParameterMessage(key, value); @@ -117,6 +121,8 @@ public void GetSetParameterMessage(string key, string value, string expectedResu result.ShouldBeEquivalentTo(new[] { expectedResult }); consoleLinesWritten.ShouldBeEquivalentTo(new List { expectedConsole }); + + environment.GetEnvironmentVariable("GitVersion_Something").ShouldBe("1.0.0"); } [Test] @@ -142,6 +148,9 @@ public void ShouldWriteIntegration() var list = new List(); + // Assert + environment.GetEnvironmentVariable("GitVersion_Major").ShouldBeNullOrWhiteSpace(); + // Act buildServer.WriteIntegration(s => { list.Add(s); }, vars); @@ -158,6 +167,8 @@ public void ShouldWriteIntegration() .ShouldBe(string.Join(Environment.NewLine, expected)); consoleLinesWritten.ShouldBeEquivalentTo(new List { "::set-env name=GitVersion_Major::1.0.0" }); + + environment.GetEnvironmentVariable("GitVersion_Major").ShouldBe("1.0.0"); } [Test] diff --git a/src/GitVersionCore/BuildServers/GitHubActions.cs b/src/GitVersionCore/BuildServers/GitHubActions.cs index bdc3eb4004..b5a99f7506 100644 --- a/src/GitVersionCore/BuildServers/GitHubActions.cs +++ b/src/GitVersionCore/BuildServers/GitHubActions.cs @@ -36,11 +36,14 @@ public override string[] GenerateSetParameterMessage(string name, string value) if (!string.IsNullOrWhiteSpace(value)) { - console.WriteLine($"::set-env name=GitVersion_{name}::{value}"); + var key = $"GitVersion_{name}"; + + Environment.SetEnvironmentVariable(key, value); + console.WriteLine($"::set-env name={key}::{value}"); return new[] { - $"Adding Environment Variable. name='GitVersion_{name}' value='{value}'" + $"Adding Environment Variable. name='{key}' value='{value}'" }; } From e345390ce2cbbeaa613618406ad94f65a26a4377 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Thu, 5 Dec 2019 10:08:03 -0500 Subject: [PATCH 14/26] Tweaking message --- src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs | 4 ++-- src/GitVersionCore/BuildServers/GitHubActions.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs b/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs index bf88bb9703..0ba8a166ab 100644 --- a/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs +++ b/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs @@ -103,7 +103,7 @@ public void GetCurrentBranchShouldNotMatchTag() } [TestCase("Something", "1.0.0", - "Adding Environment Variable. name='GitVersion_Something' value='1.0.0'", + "Adding Environment Variable to current and future steps. name='GitVersion_Something' value='1.0.0'", "::set-env name=GitVersion_Something::1.0.0")] public void GetSetParameterMessage(string key, string value, string expectedResult, string expectedConsole) { @@ -160,7 +160,7 @@ public void ShouldWriteIntegration() "Executing GenerateSetVersionMessage for 'GitHubActions'.", "", "Executing GenerateBuildLogOutput for 'GitHubActions' Keys:Major.", - "Adding Environment Variable. name='GitVersion_Major' value='1.0.0'" + "Adding Environment Variable to current and future steps. name='GitVersion_Major' value='1.0.0'" }; string.Join(Environment.NewLine, list) diff --git a/src/GitVersionCore/BuildServers/GitHubActions.cs b/src/GitVersionCore/BuildServers/GitHubActions.cs index b5a99f7506..6ff24060cd 100644 --- a/src/GitVersionCore/BuildServers/GitHubActions.cs +++ b/src/GitVersionCore/BuildServers/GitHubActions.cs @@ -43,7 +43,7 @@ public override string[] GenerateSetParameterMessage(string name, string value) return new[] { - $"Adding Environment Variable. name='{key}' value='{value}'" + $"Adding Environment Variable to current and future steps. name='{key}' value='{value}'" }; } From 70936d5d1aaed3d51410fcd571dcf4d05f188938 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Thu, 5 Dec 2019 10:08:59 -0500 Subject: [PATCH 15/26] Restoring code --- src/GitVersionCore/Common/BuildServerBase.cs | 6 +----- src/GitVersionTask/GitVersionTaskExecutor.cs | 6 +----- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/src/GitVersionCore/Common/BuildServerBase.cs b/src/GitVersionCore/Common/BuildServerBase.cs index 9611ffef21..bc0de6bba8 100644 --- a/src/GitVersionCore/Common/BuildServerBase.cs +++ b/src/GitVersionCore/Common/BuildServerBase.cs @@ -5,8 +5,6 @@ namespace GitVersion { - using System.Linq; - public abstract class BuildServerBase : IBuildServer { protected readonly ILog Log; @@ -42,11 +40,9 @@ public virtual void WriteIntegration(Action writer, VersionVariables var return; } - var keys = string.Join(";", variables.Where(pair => !string.IsNullOrWhiteSpace(pair.Value)).Select(pair => pair.Key)); - writer($"Executing GenerateSetVersionMessage for '{GetType().Name}'."); writer(GenerateSetVersionMessage(variables)); - writer($"Executing GenerateBuildLogOutput for '{GetType().Name}' Keys:{keys}."); + writer($"Executing GenerateBuildLogOutput for '{GetType().Name}'."); foreach (var buildParameter in BuildOutputFormatter.GenerateBuildLogOutput(this, variables)) { writer(buildParameter); diff --git a/src/GitVersionTask/GitVersionTaskExecutor.cs b/src/GitVersionTask/GitVersionTaskExecutor.cs index 8f57b471da..9e24f94fd6 100644 --- a/src/GitVersionTask/GitVersionTaskExecutor.cs +++ b/src/GitVersionTask/GitVersionTaskExecutor.cs @@ -8,8 +8,6 @@ namespace GitVersion.MSBuildTask { - using System.Linq; - public class GitVersionTaskExecutor : IGitVersionTaskExecutor { private readonly IFileSystem fileSystem; @@ -68,11 +66,9 @@ public void WriteVersionInfoToBuildLog(WriteVersionInfoToBuildLog task) var buildServer = buildServerResolver.Resolve(); if (buildServer != null) { - var keys = string.Join(";", versionVariables.Where(pair => !string.IsNullOrWhiteSpace(pair.Value)).Select(pair => pair.Key)); - logger.LogMessage($"Executing GenerateSetVersionMessage for '{buildServer.GetType().Name}'."); logger.LogMessage(buildServer.GenerateSetVersionMessage(versionVariables)); - logger.LogMessage($"Executing GenerateBuildLogOutput for '{GetType().Name}' Keys:{keys}."); + logger.LogMessage($"Executing GenerateBuildLogOutput for '{buildServer.GetType().Name}'."); foreach (var buildParameter in BuildOutputFormatter.GenerateBuildLogOutput(buildServer, versionVariables)) { logger.LogMessage(buildParameter); From c43ae5d878b773eec5d9ce7b9732a3c65f7b4db8 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Thu, 5 Dec 2019 10:09:55 -0500 Subject: [PATCH 16/26] Fixing test --- src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs b/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs index 0ba8a166ab..e8673010f0 100644 --- a/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs +++ b/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs @@ -159,7 +159,7 @@ public void ShouldWriteIntegration() { "Executing GenerateSetVersionMessage for 'GitHubActions'.", "", - "Executing GenerateBuildLogOutput for 'GitHubActions' Keys:Major.", + "Executing GenerateBuildLogOutput for 'GitHubActions'.", "Adding Environment Variable to current and future steps. name='GitVersion_Major' value='1.0.0'" }; From 50eed4d4ea2b8f2db880c7d1054df1c333bc64b6 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Thu, 5 Dec 2019 10:32:55 -0500 Subject: [PATCH 17/26] Trying to set the environment variable to the user instead of the process --- src/GitVersionCore.Tests/TestEnvironment.cs | 7 +++++++ src/GitVersionCore/BuildServers/GitHubActions.cs | 4 +++- src/GitVersionCore/Common/Environment.cs | 7 +++++++ src/GitVersionCore/Common/IEnvironment.cs | 3 +++ 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/GitVersionCore.Tests/TestEnvironment.cs b/src/GitVersionCore.Tests/TestEnvironment.cs index bb50eee453..7d060ecc0e 100644 --- a/src/GitVersionCore.Tests/TestEnvironment.cs +++ b/src/GitVersionCore.Tests/TestEnvironment.cs @@ -3,6 +3,8 @@ namespace GitVersionCore.Tests { + using System; + public class TestEnvironment : IEnvironment { private readonly IDictionary map; @@ -21,5 +23,10 @@ public void SetEnvironmentVariable(string variableName, string value) { map[variableName] = value; } + + public void SetEnvironmentVariable(string variableName, string value, EnvironmentVariableTarget target) + { + SetEnvironmentVariable(variableName, value); + } } } diff --git a/src/GitVersionCore/BuildServers/GitHubActions.cs b/src/GitVersionCore/BuildServers/GitHubActions.cs index 6ff24060cd..f70646f20f 100644 --- a/src/GitVersionCore/BuildServers/GitHubActions.cs +++ b/src/GitVersionCore/BuildServers/GitHubActions.cs @@ -4,6 +4,8 @@ namespace GitVersion.BuildServers { + using System; + public class GitHubActions: BuildServerBase { private readonly IConsole console; @@ -38,7 +40,7 @@ public override string[] GenerateSetParameterMessage(string name, string value) { var key = $"GitVersion_{name}"; - Environment.SetEnvironmentVariable(key, value); + Environment.SetEnvironmentVariable(key, value, EnvironmentVariableTarget.User); console.WriteLine($"::set-env name={key}::{value}"); return new[] diff --git a/src/GitVersionCore/Common/Environment.cs b/src/GitVersionCore/Common/Environment.cs index 0eb30eccc0..fedd86147f 100644 --- a/src/GitVersionCore/Common/Environment.cs +++ b/src/GitVersionCore/Common/Environment.cs @@ -1,3 +1,5 @@ +using System; + namespace GitVersion { public class Environment : IEnvironment @@ -11,5 +13,10 @@ public void SetEnvironmentVariable(string variableName, string value) { System.Environment.SetEnvironmentVariable(variableName, value); } + + public void SetEnvironmentVariable(string variableName, string value, EnvironmentVariableTarget target) + { + System.Environment.SetEnvironmentVariable(variableName, value, target); + } } } diff --git a/src/GitVersionCore/Common/IEnvironment.cs b/src/GitVersionCore/Common/IEnvironment.cs index efa0114a8b..091b4f8da2 100644 --- a/src/GitVersionCore/Common/IEnvironment.cs +++ b/src/GitVersionCore/Common/IEnvironment.cs @@ -1,8 +1,11 @@ namespace GitVersion { + using System; + public interface IEnvironment { string GetEnvironmentVariable(string variableName); void SetEnvironmentVariable(string variableName, string value); + void SetEnvironmentVariable(string variableName, string value, EnvironmentVariableTarget target); } } From ce6a07d74b046ff4de1eff88114c3b1bd13f1ee4 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Thu, 5 Dec 2019 10:37:32 -0500 Subject: [PATCH 18/26] Changing Resharper setting which relocates using import to inside namespace --- src/GitVersion.sln.DotSettings | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/GitVersion.sln.DotSettings b/src/GitVersion.sln.DotSettings index 6f32af444a..1ea5318cf3 100644 --- a/src/GitVersion.sln.DotSettings +++ b/src/GitVersion.sln.DotSettings @@ -485,7 +485,7 @@ II.2.12 <HandlesEvent /> </Patterns> CustomLayout - True + False True True False From 7c0f60b32cb2a3b8e97d55ea0dd3ae080f4000d0 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Thu, 5 Dec 2019 10:40:10 -0500 Subject: [PATCH 19/26] Fixing build errors --- src/GitVersionExe.Tests/Helpers/TestEnvironment.cs | 6 ++++++ src/GitVersionTask.Tests/Helpers/TestEnvironment.cs | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/src/GitVersionExe.Tests/Helpers/TestEnvironment.cs b/src/GitVersionExe.Tests/Helpers/TestEnvironment.cs index 0a17db0e29..25e846d6be 100644 --- a/src/GitVersionExe.Tests/Helpers/TestEnvironment.cs +++ b/src/GitVersionExe.Tests/Helpers/TestEnvironment.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using GitVersion; @@ -21,5 +22,10 @@ public void SetEnvironmentVariable(string variableName, string value) { map[variableName] = value; } + + public void SetEnvironmentVariable(string variableName, string value, EnvironmentVariableTarget target) + { + map[variableName] = value; + } } } diff --git a/src/GitVersionTask.Tests/Helpers/TestEnvironment.cs b/src/GitVersionTask.Tests/Helpers/TestEnvironment.cs index 4707ab8bc4..51869852fa 100644 --- a/src/GitVersionTask.Tests/Helpers/TestEnvironment.cs +++ b/src/GitVersionTask.Tests/Helpers/TestEnvironment.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; namespace GitVersion.MSBuildTask.Tests.Helpers @@ -20,5 +21,10 @@ public void SetEnvironmentVariable(string variableName, string value) { map[variableName] = value; } + + public void SetEnvironmentVariable(string variableName, string value, EnvironmentVariableTarget target) + { + map[variableName] = value; + } } } From 8aa86c903bd6c890cd395c791b867eae6b59260c Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Thu, 5 Dec 2019 10:55:26 -0500 Subject: [PATCH 20/26] Removing functionality to set environment variables for current step --- .../BuildServers/GitHubActionsTests.cs | 9 ++------- src/GitVersionCore.Tests/TestEnvironment.cs | 7 ------- src/GitVersionCore/BuildServers/GitHubActions.cs | 5 +---- src/GitVersionCore/Common/IEnvironment.cs | 1 - src/GitVersionExe.Tests/Helpers/TestEnvironment.cs | 6 ------ src/GitVersionTask.Tests/Helpers/TestEnvironment.cs | 6 ------ 6 files changed, 3 insertions(+), 31 deletions(-) diff --git a/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs b/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs index e8673010f0..9f238a4ec8 100644 --- a/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs +++ b/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs @@ -40,7 +40,6 @@ public void SetUp() public void TearDown() { environment.SetEnvironmentVariable("GITHUB_ACTION", null); - environment.SetEnvironmentVariable("GitVersion_Major", null); } [Test] @@ -103,7 +102,7 @@ public void GetCurrentBranchShouldNotMatchTag() } [TestCase("Something", "1.0.0", - "Adding Environment Variable to current and future steps. name='GitVersion_Something' value='1.0.0'", + "Adding Environment Variable to future steps. name='GitVersion_Something' value='1.0.0'", "::set-env name=GitVersion_Something::1.0.0")] public void GetSetParameterMessage(string key, string value, string expectedResult, string expectedConsole) { @@ -121,8 +120,6 @@ public void GetSetParameterMessage(string key, string value, string expectedResu result.ShouldBeEquivalentTo(new[] { expectedResult }); consoleLinesWritten.ShouldBeEquivalentTo(new List { expectedConsole }); - - environment.GetEnvironmentVariable("GitVersion_Something").ShouldBe("1.0.0"); } [Test] @@ -160,15 +157,13 @@ public void ShouldWriteIntegration() "Executing GenerateSetVersionMessage for 'GitHubActions'.", "", "Executing GenerateBuildLogOutput for 'GitHubActions'.", - "Adding Environment Variable to current and future steps. name='GitVersion_Major' value='1.0.0'" + "Adding Environment Variable to future steps. name='GitVersion_Major' value='1.0.0'" }; string.Join(Environment.NewLine, list) .ShouldBe(string.Join(Environment.NewLine, expected)); consoleLinesWritten.ShouldBeEquivalentTo(new List { "::set-env name=GitVersion_Major::1.0.0" }); - - environment.GetEnvironmentVariable("GitVersion_Major").ShouldBe("1.0.0"); } [Test] diff --git a/src/GitVersionCore.Tests/TestEnvironment.cs b/src/GitVersionCore.Tests/TestEnvironment.cs index 7d060ecc0e..bb50eee453 100644 --- a/src/GitVersionCore.Tests/TestEnvironment.cs +++ b/src/GitVersionCore.Tests/TestEnvironment.cs @@ -3,8 +3,6 @@ namespace GitVersionCore.Tests { - using System; - public class TestEnvironment : IEnvironment { private readonly IDictionary map; @@ -23,10 +21,5 @@ public void SetEnvironmentVariable(string variableName, string value) { map[variableName] = value; } - - public void SetEnvironmentVariable(string variableName, string value, EnvironmentVariableTarget target) - { - SetEnvironmentVariable(variableName, value); - } } } diff --git a/src/GitVersionCore/BuildServers/GitHubActions.cs b/src/GitVersionCore/BuildServers/GitHubActions.cs index f70646f20f..8770420c3c 100644 --- a/src/GitVersionCore/BuildServers/GitHubActions.cs +++ b/src/GitVersionCore/BuildServers/GitHubActions.cs @@ -4,8 +4,6 @@ namespace GitVersion.BuildServers { - using System; - public class GitHubActions: BuildServerBase { private readonly IConsole console; @@ -40,12 +38,11 @@ public override string[] GenerateSetParameterMessage(string name, string value) { var key = $"GitVersion_{name}"; - Environment.SetEnvironmentVariable(key, value, EnvironmentVariableTarget.User); console.WriteLine($"::set-env name={key}::{value}"); return new[] { - $"Adding Environment Variable to current and future steps. name='{key}' value='{value}'" + $"Adding Environment Variable to future steps. name='{key}' value='{value}'" }; } diff --git a/src/GitVersionCore/Common/IEnvironment.cs b/src/GitVersionCore/Common/IEnvironment.cs index 091b4f8da2..74dd841551 100644 --- a/src/GitVersionCore/Common/IEnvironment.cs +++ b/src/GitVersionCore/Common/IEnvironment.cs @@ -6,6 +6,5 @@ public interface IEnvironment { string GetEnvironmentVariable(string variableName); void SetEnvironmentVariable(string variableName, string value); - void SetEnvironmentVariable(string variableName, string value, EnvironmentVariableTarget target); } } diff --git a/src/GitVersionExe.Tests/Helpers/TestEnvironment.cs b/src/GitVersionExe.Tests/Helpers/TestEnvironment.cs index 25e846d6be..0a17db0e29 100644 --- a/src/GitVersionExe.Tests/Helpers/TestEnvironment.cs +++ b/src/GitVersionExe.Tests/Helpers/TestEnvironment.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Generic; using GitVersion; @@ -22,10 +21,5 @@ public void SetEnvironmentVariable(string variableName, string value) { map[variableName] = value; } - - public void SetEnvironmentVariable(string variableName, string value, EnvironmentVariableTarget target) - { - map[variableName] = value; - } } } diff --git a/src/GitVersionTask.Tests/Helpers/TestEnvironment.cs b/src/GitVersionTask.Tests/Helpers/TestEnvironment.cs index 51869852fa..4707ab8bc4 100644 --- a/src/GitVersionTask.Tests/Helpers/TestEnvironment.cs +++ b/src/GitVersionTask.Tests/Helpers/TestEnvironment.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Generic; namespace GitVersion.MSBuildTask.Tests.Helpers @@ -21,10 +20,5 @@ public void SetEnvironmentVariable(string variableName, string value) { map[variableName] = value; } - - public void SetEnvironmentVariable(string variableName, string value, EnvironmentVariableTarget target) - { - map[variableName] = value; - } } } From e6d95eb422b472a2d95126c40d7433f175b57b8c Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Thu, 5 Dec 2019 11:32:06 -0500 Subject: [PATCH 21/26] Cleanup --- src/GitVersionCore/Common/IEnvironment.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/GitVersionCore/Common/IEnvironment.cs b/src/GitVersionCore/Common/IEnvironment.cs index 74dd841551..efa0114a8b 100644 --- a/src/GitVersionCore/Common/IEnvironment.cs +++ b/src/GitVersionCore/Common/IEnvironment.cs @@ -1,7 +1,5 @@ namespace GitVersion { - using System; - public interface IEnvironment { string GetEnvironmentVariable(string variableName); From 80df6afe0f421c04e73add8fcb51496557b39afb Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Mon, 16 Dec 2019 08:49:04 -0500 Subject: [PATCH 22/26] Cleanup --- src/GitVersionCore/Common/Environment.cs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/GitVersionCore/Common/Environment.cs b/src/GitVersionCore/Common/Environment.cs index fedd86147f..d7066f931f 100644 --- a/src/GitVersionCore/Common/Environment.cs +++ b/src/GitVersionCore/Common/Environment.cs @@ -13,10 +13,5 @@ public void SetEnvironmentVariable(string variableName, string value) { System.Environment.SetEnvironmentVariable(variableName, value); } - - public void SetEnvironmentVariable(string variableName, string value, EnvironmentVariableTarget target) - { - System.Environment.SetEnvironmentVariable(variableName, value, target); - } } } From 3f314257a5d31736a61ad0590860b7cc6c8126d5 Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Mon, 16 Dec 2019 08:55:06 -0500 Subject: [PATCH 23/26] Remove console --- .../BuildServers/GitHubActionsTests.cs | 16 ++++++++-------- src/GitVersionCore/BuildServers/GitHubActions.cs | 9 ++------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs b/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs index 9f238a4ec8..dafd55d2da 100644 --- a/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs +++ b/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs @@ -46,7 +46,7 @@ public void TearDown() public void CanApplyToCurrentContextShouldBeTrueWhenEnvironmentVariableIsSet() { // Arrange - var buildServer = new GitHubActions(environment, log, console); + var buildServer = new GitHubActions(environment, log); // Act var result = buildServer.CanApplyToCurrentContext(); @@ -60,7 +60,7 @@ public void CanApplyToCurrentContextShouldBeFalseWhenEnvironmentVariableIsNotSet { // Arrange environment.SetEnvironmentVariable("GITHUB_ACTION", ""); - var buildServer = new GitHubActions(environment, log, console); + var buildServer = new GitHubActions(environment, log); // Act var result = buildServer.CanApplyToCurrentContext(); @@ -77,7 +77,7 @@ public void GetCurrentBranchShouldGetBranchIfSet() environment.SetEnvironmentVariable("GITHUB_REF", $"refs/heads/{expected}"); - var buildServer = new GitHubActions(environment, log, console); + var buildServer = new GitHubActions(environment, log); // Act var result = buildServer.GetCurrentBranch(false); @@ -92,7 +92,7 @@ public void GetCurrentBranchShouldNotMatchTag() // Arrange environment.SetEnvironmentVariable("GITHUB_REF", $"refs/tags/v1.0.0"); - var buildServer = new GitHubActions(environment, log, console); + var buildServer = new GitHubActions(environment, log); // Act var result = buildServer.GetCurrentBranch(false); @@ -107,7 +107,7 @@ public void GetCurrentBranchShouldNotMatchTag() public void GetSetParameterMessage(string key, string value, string expectedResult, string expectedConsole) { // Arrange - var buildServer = new GitHubActions(environment, log, console); + var buildServer = new GitHubActions(environment, log); // Assert environment.GetEnvironmentVariable("GitVersion_Something").ShouldBeNullOrWhiteSpace(); @@ -126,7 +126,7 @@ public void GetSetParameterMessage(string key, string value, string expectedResu public void SkipEmptySetParameterMessage() { // Arrange - var buildServer = new GitHubActions(environment, log, console); + var buildServer = new GitHubActions(environment, log); // Act var result = buildServer.GenerateSetParameterMessage("Hello", string.Empty); @@ -139,7 +139,7 @@ public void SkipEmptySetParameterMessage() public void ShouldWriteIntegration() { // Arrange - var buildServer = new GitHubActions(environment, log, console); + var buildServer = new GitHubActions(environment, log); var vars = new TestableVersionVariables("1.0.0"); @@ -170,7 +170,7 @@ public void ShouldWriteIntegration() public void GetEmptyGenerateSetVersionMessage() { // Arrange - var buildServer = new GitHubActions(environment, log, console); + var buildServer = new GitHubActions(environment, log); var vars = new TestableVersionVariables("1.0.0"); // Act diff --git a/src/GitVersionCore/BuildServers/GitHubActions.cs b/src/GitVersionCore/BuildServers/GitHubActions.cs index 8770420c3c..c5aca771fc 100644 --- a/src/GitVersionCore/BuildServers/GitHubActions.cs +++ b/src/GitVersionCore/BuildServers/GitHubActions.cs @@ -1,17 +1,14 @@ using GitVersion.Logging; using GitVersion.OutputVariables; -using GitVersion.Configuration; namespace GitVersion.BuildServers { public class GitHubActions: BuildServerBase { - private readonly IConsole console; // https://help.github.com/en/actions/automating-your-workflow-with-github-actions/using-environment-variables#default-environment-variables - public GitHubActions(IEnvironment environment, ILog log, IConsole console) : base(environment, log) + public GitHubActions(IEnvironment environment, ILog log) : base(environment, log) { - this.console = console; } public const string EnvironmentVariableName = "GITHUB_ACTION"; @@ -38,11 +35,9 @@ public override string[] GenerateSetParameterMessage(string name, string value) { var key = $"GitVersion_{name}"; - console.WriteLine($"::set-env name={key}::{value}"); - return new[] { - $"Adding Environment Variable to future steps. name='{key}' value='{value}'" + $"::set-env name={key}::{value}" }; } From c896a85d3b3ee0df79347b93437a80770481063c Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Mon, 16 Dec 2019 08:59:10 -0500 Subject: [PATCH 24/26] Fixing tests --- .../BuildServers/GitHubActionsTests.cs | 20 ++----------------- 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs b/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs index dafd55d2da..e587b1fada 100644 --- a/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs +++ b/src/GitVersionCore.Tests/BuildServers/GitHubActionsTests.cs @@ -5,8 +5,6 @@ using NUnit.Framework; using Shouldly; using System.Collections.Generic; -using GitVersion.Configuration; -using NSubstitute; using Environment = System.Environment; namespace GitVersionCore.Tests.BuildServers @@ -15,22 +13,13 @@ namespace GitVersionCore.Tests.BuildServers [TestFixture] public class GitHubActionsTests : TestBase { - private IConsole console; private IEnvironment environment; private ILog log; - private List consoleLinesWritten; [SetUp] public void SetUp() { log = new NullLog(); - console = Substitute.For(); - - consoleLinesWritten = new List(); - console.WhenForAnyArgs(c => c.WriteLine(default)) - .Do(info => consoleLinesWritten.Add(info.Arg())); - console.WhenForAnyArgs(c => c.WriteLine()) - .Do(info => consoleLinesWritten.Add(string.Empty)); environment = new TestEnvironment(); environment.SetEnvironmentVariable("GITHUB_ACTION", Guid.NewGuid().ToString()); @@ -102,9 +91,8 @@ public void GetCurrentBranchShouldNotMatchTag() } [TestCase("Something", "1.0.0", - "Adding Environment Variable to future steps. name='GitVersion_Something' value='1.0.0'", "::set-env name=GitVersion_Something::1.0.0")] - public void GetSetParameterMessage(string key, string value, string expectedResult, string expectedConsole) + public void GetSetParameterMessage(string key, string value, string expectedResult) { // Arrange var buildServer = new GitHubActions(environment, log); @@ -118,8 +106,6 @@ public void GetSetParameterMessage(string key, string value, string expectedResu // Assert result.ShouldContain(s => true, 1); result.ShouldBeEquivalentTo(new[] { expectedResult }); - - consoleLinesWritten.ShouldBeEquivalentTo(new List { expectedConsole }); } [Test] @@ -157,13 +143,11 @@ public void ShouldWriteIntegration() "Executing GenerateSetVersionMessage for 'GitHubActions'.", "", "Executing GenerateBuildLogOutput for 'GitHubActions'.", - "Adding Environment Variable to future steps. name='GitVersion_Major' value='1.0.0'" + "::set-env name=GitVersion_Major::1.0.0" }; string.Join(Environment.NewLine, list) .ShouldBe(string.Join(Environment.NewLine, expected)); - - consoleLinesWritten.ShouldBeEquivalentTo(new List { "::set-env name=GitVersion_Major::1.0.0" }); } [Test] From 9b42b52068ba615508cbb1b2af8d7542a4a1511c Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Tue, 17 Dec 2019 16:22:15 -0500 Subject: [PATCH 25/26] Removing redundant override --- src/GitVersionCore/BuildServers/GitHubActions.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/GitVersionCore/BuildServers/GitHubActions.cs b/src/GitVersionCore/BuildServers/GitHubActions.cs index c5aca771fc..f04dbc486b 100644 --- a/src/GitVersionCore/BuildServers/GitHubActions.cs +++ b/src/GitVersionCore/BuildServers/GitHubActions.cs @@ -13,10 +13,6 @@ public GitHubActions(IEnvironment environment, ILog log) : base(environment, log public const string EnvironmentVariableName = "GITHUB_ACTION"; protected override string EnvironmentVariable { get; } = EnvironmentVariableName; - public override bool CanApplyToCurrentContext() - { - return !string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable(EnvironmentVariable)); - } public override string GenerateSetVersionMessage(VersionVariables variables) { From 926e3a6da815a83f24f4d79f3517e7bdda09330c Mon Sep 17 00:00:00 2001 From: Stanley Goldman Date: Tue, 17 Dec 2019 16:23:13 -0500 Subject: [PATCH 26/26] Cleanup --- src/GitVersionCore/Common/Environment.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/GitVersionCore/Common/Environment.cs b/src/GitVersionCore/Common/Environment.cs index d7066f931f..0eb30eccc0 100644 --- a/src/GitVersionCore/Common/Environment.cs +++ b/src/GitVersionCore/Common/Environment.cs @@ -1,5 +1,3 @@ -using System; - namespace GitVersion { public class Environment : IEnvironment