This repository was archived by the owner on Jan 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 94
Improves unit test results stability across runs #107
Open
fourpastmidnight
wants to merge
3
commits into
OctopusDeploy:master
Choose a base branch
from
fourpastmidnight:improve-unit-test-results-stability-across-runs
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -63,3 +63,5 @@ source/Samples/.octopack | |
| _ReSharper* | ||
| source/Samples/packages | ||
| !source/build | ||
| _NCrunch_*/ | ||
| *.v3.ncrunch* | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
source/OctoPack.Tests/Installation/OctoPackInstallationTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| using System; | ||
| using System.Diagnostics; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using Microsoft.Build.Evaluation; | ||
| using NUnit.Framework; | ||
|
|
||
| namespace OctoPack.Tests.Installation | ||
| { | ||
| [TestFixture] | ||
| public class OctoPackInstallationTests | ||
| { | ||
|
|
||
|
|
||
| public static string Root { get; private set; } | ||
| public static string SampleProjectDirectory { get; private set; } | ||
| public static string TestProjectFile { get; private set; } | ||
|
|
||
| [SetUp] | ||
| public static void SetUp() | ||
| { | ||
| Root = new Uri(typeof(OctoPackInstallationTests).Assembly.CodeBase).LocalPath; | ||
| while (!(Root.EndsWith("source") || Root.EndsWith("source\\"))) | ||
| { | ||
| Root = Path.GetFullPath(Path.Combine(Root, "..\\")); | ||
| } | ||
|
|
||
| SampleProjectDirectory = Path.Combine(Root, "OctoPack.Tests.SampleGitVersionAssembly"); | ||
|
|
||
| TestProjectFile = Path.Combine(SampleProjectDirectory, "OctoPack.Tests.SampleGitVersionAssembly.csproj").CreateTestProjectFile(); | ||
| } | ||
|
|
||
| [TearDown] | ||
| public static void TearDown() | ||
| { | ||
| try | ||
| { | ||
| File.Delete(TestProjectFile); | ||
| } | ||
| catch | ||
| { | ||
| // Swallow any exception--just making an attempt to clean up after ourselves. | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public void ProperlyInstallsOctoPackIntoProjects() | ||
| { | ||
| var targetsFile = Path.Combine(SampleProjectDirectory, "build", "OctoPack.targets"); | ||
|
|
||
| bool result = TestProjectFile.InstallOctoPack(); | ||
|
|
||
| Assert.That(result, Is.True, "Failed to install OctoPack into the project file '{0}'.", TestProjectFile); | ||
|
|
||
| var projectCollection = new ProjectCollection(); | ||
| var project = projectCollection.LoadProject(TestProjectFile); | ||
| var imports = project.Xml.Imports.Where(i => i.Project.Contains("OctoPack")); | ||
| var octoPackTargetsImport = imports.Last(); | ||
| Assert.That(octoPackTargetsImport.Project, Is.StringEnding(project.GetRelativePathToFile(targetsFile))); | ||
| } | ||
|
|
||
| [Test] | ||
| public void ProperlyUninstallsOctoPackFromProjects() | ||
| { | ||
| TestProjectFile.InstallOctoPack(); | ||
|
|
||
| var result = TestProjectFile.UninstallOctoPack(); | ||
|
|
||
| var projectCollection = new ProjectCollection(); | ||
| var project = projectCollection.LoadProject(TestProjectFile); | ||
|
|
||
| Assert.That(project.Xml.Imports.Count, Is.EqualTo(2), project.Xml.RawXml); | ||
| Assert.That(project.Xml.Imports.First().Project, Is.StringEnding("Microsoft.Common.props")); | ||
| Assert.That(project.Xml.Imports.Last().Project, Is.StringEnding("Microsoft.CSharp.targets")); | ||
| } | ||
| } | ||
|
|
||
| static class TestHelpers | ||
| { | ||
| private const string TestScript = @"& { | ||
| Param( | ||
| [Parameter(Mandatory, Position = 0)] | ||
| [string]$ProjectPath, | ||
| [Parameter(Mandatory, Position = 1)] | ||
| [string]$ToolsPath, | ||
| [Parameter(Mandatory, Position = 2)] | ||
| [ValidateSet('Install','Uninstall')] | ||
| [string]$Operation | ||
| ) | ||
| Add-Type -AssemblyName 'Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'; | ||
| $package = [PSCustomObject]@{ Id = 'OctoPack' }; | ||
| $project = [Microsoft.Build.Evaluation.ProjectCollection]::GlobalProjectCollection.LoadProject($ProjectPath) | | ||
| Select-Object -First 1 | | ||
| Add-Member -MemberType AliasProperty -Name FullName -Value FullPath -PassThru; | ||
| & """"""$ToolsPath\${Operation}.ps1"""""" -toolsPath $ToolsPath -package $package -project $project; | ||
| } | ||
| "; | ||
|
|
||
| private const string Arguments = "-NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command \"{0}\" \"{1}\" \"{2}\" {3}"; | ||
|
|
||
| public static string CreateTestProjectFile(this string projectFilePath) | ||
| { | ||
| var projectFileDirectory = Path.GetDirectoryName(projectFilePath); | ||
| var projectFileFilename = Path.GetFileNameWithoutExtension(projectFilePath); | ||
|
|
||
| var temporaryProjectFilePath = Path.Combine(projectFileDirectory, string.Concat(projectFileFilename, "-Temp.csproj")); | ||
| if (File.Exists(temporaryProjectFilePath)) | ||
| { | ||
| File.Delete(temporaryProjectFilePath); | ||
| } | ||
|
|
||
| File.Copy(projectFilePath, temporaryProjectFilePath, true); | ||
|
|
||
| return temporaryProjectFilePath; | ||
| } | ||
|
|
||
| public static string GetRelativePathToFile(this Project project, string absolutePath) | ||
| { | ||
| return Uri.UnescapeDataString( | ||
| new Uri(project.FullPath, UriKind.Absolute) | ||
| .MakeRelativeUri(new Uri(absolutePath, UriKind.Absolute)) | ||
| .ToString() | ||
| ) | ||
| .Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar); | ||
| } | ||
|
|
||
| public static bool InstallOctoPack(this string projectFilePath) => projectFilePath.ModifyProject("Install"); | ||
|
|
||
| public static bool UninstallOctoPack(this string projectFilePath) => projectFilePath.ModifyProject("Uninstall"); | ||
|
|
||
| private static bool ModifyProject(this string projectFilePath, string operation) | ||
| { | ||
| var toolsDirectoryPath = Path.Combine(OctoPackInstallationTests.Root, "tools"); | ||
|
|
||
| var startInfo = new ProcessStartInfo | ||
| { | ||
| FileName = "powershell.exe", | ||
| Arguments = string.Format( | ||
| Arguments, | ||
| TestScript.Replace(Environment.NewLine, " ").Trim(), | ||
| projectFilePath, | ||
| toolsDirectoryPath, | ||
| operation), | ||
| UseShellExecute = false, | ||
| RedirectStandardOutput = true, | ||
| RedirectStandardError = true, | ||
| WorkingDirectory = OctoPackInstallationTests.SampleProjectDirectory, | ||
| LoadUserProfile = false, | ||
| CreateNoWindow = true | ||
| }; | ||
|
|
||
| var process = Process.Start(startInfo); | ||
| process.WaitForExit(); | ||
| Console.Out.Write(process.StandardOutput.ReadToEnd()); | ||
| Console.Error.Write(process.StandardError.ReadToEnd()); | ||
|
|
||
| var exitCode = process.ExitCode; | ||
|
|
||
| process.Close(); | ||
|
|
||
| return exitCode == 0; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't run the tests via
build.cmdor via my IDE with this test adapter.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the Test Adapter is only used when running the tests from within Visual Studio.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I amend my former comment. The test adapter is used when running the tests using vstest.console.exe (which internally, Visual Studio uses to run the tests, too).
Now, when it comes to Cake, I don't know how they implemented their NUnit function. Perhaps it uses the Nunit console runner?