Skip to content
This repository was archived by the owner on Jan 20, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,5 @@ source/Samples/.octopack
_ReSharper*
source/Samples/packages
!source/build
_NCrunch_*/
*.v3.ncrunch*
21 changes: 1 addition & 20 deletions source/OctoPack.Tasks/OctoPack.Tasks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,34 +64,15 @@
<Compile Include="Util\VersionExtensions.cs" />
<Compile Include="Util\XmlElementExtensions.cs" />
</ItemGroup>
<ItemGroup>
<None Include="..\build\OctoPack.targets">
<Link>OctoPack.targets</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<SubType>Designer</SubType>
</None>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<None Include="..\build\NuGet.exe">
<Link>NuGet.exe</Link>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>dir
xcopy * ..\..\..\Samples\.octopack\ /Y
copy nuget.exe ..\..\..\Samples\.nuget\</PostBuildEvent>
</PropertyGroup>
<Import Project="..\packages\GitVersionTask.3.6.2\build\portable-net+sl+win+wpa+wp\GitVersionTask.targets" Condition="Exists('..\packages\GitVersionTask.3.6.2\build\portable-net+sl+win+wpa+wp\GitVersionTask.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\GitVersionTask.3.6.2\build\portable-net+sl+win+wpa+wp\GitVersionTask.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\GitVersionTask.3.6.2\build\portable-net+sl+win+wpa+wp\GitVersionTask.targets'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
Expand Down
164 changes: 164 additions & 0 deletions source/OctoPack.Tests/Installation/OctoPackInstallationTests.cs
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;
}
}
}
13 changes: 13 additions & 0 deletions source/OctoPack.Tests/Integration/BuildFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using NuGet.Packaging;
using NUnit.Framework;
Expand Down Expand Up @@ -99,6 +100,18 @@ protected static void Clean(string path)
[TearDown]
public void TearDown()
{
try
{
Directory.EnumerateFiles(Path.Combine(Environment.CurrentDirectory, "Sample.TypeScriptApp", "Scripts"))
.Where(f => f.EndsWith(".js") || f.EndsWith(".js.map"))
.ToList()
.ForEach(f => File.Delete(f));
}
catch
{
// Swallow -- make an attempt to clean up files which can cause problems across test runs.
}

Environment.CurrentDirectory = originalDirectory;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ public void ShouldSupportWeirdTeamCityStuff()
[Test]
public void ShouldReportOutDirPackageLocationToTeamCity()
{
File.Copy("Sample.ConsoleApp\\Sample.ConsoleApp.csproj", "Sample.ConsoleApp\\Sample.ConsoleApp.csproj.teamcity", true);

MsBuild("Sample.ConsoleApp\\Sample.ConsoleApp.csproj.teamcity /p:RunOctoPack=true /p:OctoPackPackageVersion=1.0.10 /p:OctoPackPublishPackagesToTeamCity=true /p:Configuration=Release /v:m",
output => Assert.That(output, Is.StringMatching(@"##teamcity\[publishArtifacts .*\\bin\\Release\\Sample.ConsoleApp.1.0.10.nupkg'\]")),
environmentVariables: new Dictionary<string, string> { { "TEAMCITY_VERSION", "10.0" } });
Expand Down
41 changes: 36 additions & 5 deletions source/OctoPack.Tests/OctoPack.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\NUnitTestAdapter.2.3.0\build\NUnitTestAdapter.props" Condition="Exists('..\packages\NUnitTestAdapter.2.3.0\build\NUnitTestAdapter.props')" />
Copy link
Contributor

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.cmd or via my IDE with this test adapter.

Copy link
Contributor Author

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.

Copy link
Contributor Author

@fourpastmidnight fourpastmidnight Aug 25, 2020

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?

<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{2AAFB125-1A9F-45E6-A11C-770132432F16}</ProjectGuid>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>OctoPack.Tests</RootNamespace>
Expand Down Expand Up @@ -37,6 +39,7 @@
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Build" />
<Reference Include="Microsoft.Build.Framework" />
<Reference Include="Microsoft.Web.XmlTransform">
<HintPath>..\packages\Microsoft.Web.Xdt.1.0.0\lib\net40\Microsoft.Web.XmlTransform.dll</HintPath>
Expand Down Expand Up @@ -94,6 +97,7 @@
<Compile Include="..\Solution Items\VersionInfo.cs">
<Link>Properties\VersionInfo.cs</Link>
</Compile>
<Compile Include="Installation\OctoPackInstallationTests.cs" />
<Compile Include="Integration\BuildFixture.cs" />
<Compile Include="Integration\SampleSolutionBuildFixture.cs" />
<Compile Include="Tasks\AssemblyExtensionsTests.cs" />
Expand All @@ -103,15 +107,24 @@
<Compile Include="Tasks\OctopusPhysicalFileSystemTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup />
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\OctoPack.Tasks\OctoPack.Tasks.csproj">
<Project>{BEF74811-0C8D-45D4-B9DE-232795A3A84D}</Project>
<Name>OctoPack.Tasks</Name>
</ProjectReference>
<ProjectReference Include="..\OctoPack.Tests.NonGitVersionAssembly\OctoPack.Tests.NonGitVersionAssembly.csproj">
<Project>{45d3ed88-922a-4c2f-a8ed-67722fc1e709}</Project>
<Name>OctoPack.Tests.NonGitVersionAssembly</Name>
<Private>True</Private>
</ProjectReference>
<ProjectReference Include="..\OctoPack.Tests.SampleGitVersionAssembly\OctoPack.Tests.SampleGitVersionAssembly.csproj">
<Project>{7c18cb5f-1144-4476-83ce-ef387472a5f6}</Project>
<Name>OctoPack.Tests.SampleGitVersionAssembly</Name>
<Private>True</Private>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\GitVersionTask.3.6.2\build\dotnet\GitVersionTask.targets" Condition="Exists('..\packages\GitVersionTask.3.6.2\build\dotnet\GitVersionTask.targets')" />
Expand All @@ -120,12 +133,30 @@
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\GitVersionTask.3.6.2\build\dotnet\GitVersionTask.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\GitVersionTask.3.6.2\build\dotnet\GitVersionTask.targets'))" />
<Error Condition="!Exists('..\packages\NUnitTestAdapter.2.3.0\build\NUnitTestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\NUnitTestAdapter.2.3.0\build\NUnitTestAdapter.props'))" />
</Target>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
<PropertyGroup>
<BuildDependsOn>
$(BuildDependsOn);
OctoPackSlnCopyItemsForTests
</BuildDependsOn>
</PropertyGroup>
<Target Name="OctoPackSlnCopyItemsForTests">
<PropertyGroup>
<OctoPackSlnBuildDirectory>$(MSBuildThisFileDirectory)\..\build\</OctoPackSlnBuildDirectory>
<OctoPackSlnSamplesDirectory>$(MSBuildThisFileDirectory)\..\Samples\</OctoPackSlnSamplesDirectory>
</PropertyGroup>
<CreateItem Include="$(OctoPackSlnBuildDirectory)Octopack.*;$(OctoPackSlnBuildDirectory)Nuget.exe;$(OutDir)OctoPack.Tasks.dll">
<Output TaskParameter="Include" ItemName="OctoPackTestsFilesToCopy" />
</CreateItem>
<Copy SourceFiles="@(OctoPackTestsFilesToCopy)" DestinationFolder="$(OctoPackSlnSamplesDirectory).octopack" />
<Copy SourceFiles="$(OctoPackSlnBuildDirectory)Nuget.exe" DestinationFolder="$(OctoPackSlnSamplesDirectory).nuget" />
</Target>
</Project>
32 changes: 7 additions & 25 deletions source/OctoPack.Tests/Tasks/AssemblyExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.IO;
using System.Reflection;
using System.Reflection;
using NUnit.Framework;
using GitVerAsm = OctoPack.Tests.SampleGitVersionAssembly;
using NoGitVerAsm = OctoPack.Tests.NonGitVersionAssembly;

namespace OctoPack.Tests.Tasks
{
Expand All @@ -10,44 +11,25 @@ public class AssemblyExtensionsTests
[Test]
public void AssertAssemblyVersion_WhereNoGitVersionProperty_ReturnsNull()
{
var assemblyPath = GetAssemblyFullPath("OctoPack.Tests.NonGitVersionAssembly");
var assemblyPath = Assembly.GetAssembly(typeof(NoGitVerAsm.ClassReferencingDependency)).FullLocalPath();
var gitversion = AssemblyExtensions.GetNuGetVersionFromGitVersionInformation(assemblyPath);
Assert.That(gitversion, Is.Null);
}

[Test]
public void AssertAssemblyVersion_WhereDependentAssemblyOnlyInSource_GetsGitVersion()
{
var assemblyPath = GetAssemblyFullPath("OctoPack.Tests.SampleGitVersionAssembly");
var assemblyPath = Assembly.GetAssembly(typeof(GitVerAsm.ClassReferencingDependency)).FullLocalPath();
var gitversion = AssemblyExtensions.GetNuGetVersionFromGitVersionInformation(assemblyPath);
Assert.That(gitversion, Is.EqualTo("1.1.1-tests"));
}

[Test]
public void AssertAssembly_WhereDependentAssemblyNotEventInSource_GetsGitVersion()
public void AssertAssembly_WhereDependentAssemblyNotEvenInSource_GetsGitVersion()
{
var assemblyPath = GetAssemblyFullExceptionPath("OctoPack.Tests.SampleGitVersionAssembly");
var assemblyPath = Assembly.GetAssembly(typeof(GitVerAsm.ClassReferencingDependency)).FullLocalPath();
var gitversion = AssemblyExtensions.GetNuGetVersionFromGitVersionInformation(assemblyPath);
Assert.That(gitversion, Is.EqualTo("1.1.1-tests"));
}

private string GetAsseblyLocation(string name)
{
var currentAssemblyPath = Assembly.GetExecutingAssembly().FullLocalPath();
var configuration = new FileInfo(currentAssemblyPath).Directory.Name;
return Path.Combine(currentAssemblyPath, "..", "..", "..", "..", name, "bin", configuration);
}

private string GetAssemblyFullPath(string name)
{
var assemblyPath = Path.Combine(GetAsseblyLocation(name), $"{name}.dll");
return new FileInfo(assemblyPath).FullName;
}

private string GetAssemblyFullExceptionPath(string name)
{
var assemblyPath = Path.Combine(GetAsseblyLocation(name), "StandAloneDll", $"{ name}.dll");
return new FileInfo(assemblyPath).FullName;
}
}
}
Loading