Skip to content
This repository was archived by the owner on Sep 13, 2019. It is now read-only.

Commit 2203b64

Browse files
committed
Initial commit
0 parents  commit 2203b64

39 files changed

+4370
-0
lines changed

.gitattributes

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto
3+
4+
# Custom for Visual Studio
5+
*.cs diff=csharp
6+
7+
# Standard to msysgit
8+
*.doc diff=astextplain
9+
*.DOC diff=astextplain
10+
*.docx diff=astextplain
11+
*.DOCX diff=astextplain
12+
*.dot diff=astextplain
13+
*.DOT diff=astextplain
14+
*.pdf diff=astextplain
15+
*.PDF diff=astextplain
16+
*.rtf diff=astextplain
17+
*.RTF diff=astextplain

.gitignore

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Visual Studio
2+
[Oo]bj
3+
[Bb]in
4+
*.suo
5+
*.Publish.xml
6+
App_Data
7+
packages
8+
# Visual Studio 2015
9+
.vs
10+
11+
# merge
12+
*.orig
13+
*.REMOTE.*
14+
*.BASE.*
15+
*.BACKUP.*
16+
*.LOCAL.*
17+
18+
# ReSharper
19+
_ReSharper.*
20+
*.ReSharper
21+
*.user
22+
23+
# DotCover
24+
*.dotCover
25+
26+
#nuget
27+
*.nupkg
28+

.nuget/NuGet.Config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<solution>
4+
<add key="disableSourceControlIntegration" value="true" />
5+
</solution>
6+
</configuration>

.nuget/NuGet.exe

1.61 MB
Binary file not shown.

.nuget/NuGet.targets

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">$(MSBuildProjectDirectory)\..\</SolutionDir>
5+
6+
<!-- Enable the restore command to run before builds -->
7+
<RestorePackages Condition=" '$(RestorePackages)' == '' ">false</RestorePackages>
8+
9+
<!-- Property that enables building a package from a project -->
10+
<BuildPackage Condition=" '$(BuildPackage)' == '' ">false</BuildPackage>
11+
12+
<!-- Determines if package restore consent is required to restore packages -->
13+
<RequireRestoreConsent Condition=" '$(RequireRestoreConsent)' != 'false' ">true</RequireRestoreConsent>
14+
15+
<!-- Download NuGet.exe if it does not already exist -->
16+
<DownloadNuGetExe Condition=" '$(DownloadNuGetExe)' == '' ">false</DownloadNuGetExe>
17+
</PropertyGroup>
18+
19+
<ItemGroup Condition=" '$(PackageSources)' == '' ">
20+
<!-- Package sources used to restore packages. By default, registered sources under %APPDATA%\NuGet\NuGet.Config will be used -->
21+
<!-- The official NuGet package source (https://www.nuget.org/api/v2/) will be excluded if package sources are specified and it does not appear in the list -->
22+
<!--
23+
<PackageSource Include="https://www.nuget.org/api/v2/" />
24+
<PackageSource Include="https://my-nuget-source/nuget/" />
25+
-->
26+
</ItemGroup>
27+
28+
<PropertyGroup Condition=" '$(OS)' == 'Windows_NT'">
29+
<!-- Windows specific commands -->
30+
<NuGetToolsPath>$([System.IO.Path]::Combine($(SolutionDir), ".nuget"))</NuGetToolsPath>
31+
</PropertyGroup>
32+
33+
<PropertyGroup Condition=" '$(OS)' != 'Windows_NT'">
34+
<!-- We need to launch nuget.exe with the mono command if we're not on windows -->
35+
<NuGetToolsPath>$(SolutionDir).nuget</NuGetToolsPath>
36+
</PropertyGroup>
37+
38+
<PropertyGroup>
39+
<PackagesProjectConfig Condition=" '$(OS)' == 'Windows_NT'">$(MSBuildProjectDirectory)\packages.$(MSBuildProjectName.Replace(' ', '_')).config</PackagesProjectConfig>
40+
<PackagesProjectConfig Condition=" '$(OS)' != 'Windows_NT'">$(MSBuildProjectDirectory)\packages.$(MSBuildProjectName).config</PackagesProjectConfig>
41+
</PropertyGroup>
42+
43+
<PropertyGroup>
44+
<PackagesConfig Condition="Exists('$(MSBuildProjectDirectory)\packages.config')">$(MSBuildProjectDirectory)\packages.config</PackagesConfig>
45+
<PackagesConfig Condition="Exists('$(PackagesProjectConfig)')">$(PackagesProjectConfig)</PackagesConfig>
46+
</PropertyGroup>
47+
48+
<PropertyGroup>
49+
<!-- NuGet command -->
50+
<NuGetExePath Condition=" '$(NuGetExePath)' == '' ">$(NuGetToolsPath)\NuGet.exe</NuGetExePath>
51+
<PackageSources Condition=" $(PackageSources) == '' ">@(PackageSource)</PackageSources>
52+
53+
<NuGetCommand Condition=" '$(OS)' == 'Windows_NT'">"$(NuGetExePath)"</NuGetCommand>
54+
<NuGetCommand Condition=" '$(OS)' != 'Windows_NT' ">mono --runtime=v4.0.30319 "$(NuGetExePath)"</NuGetCommand>
55+
56+
<PackageOutputDir Condition="$(PackageOutputDir) == ''">$(TargetDir.Trim('\\'))</PackageOutputDir>
57+
58+
<RequireConsentSwitch Condition=" $(RequireRestoreConsent) == 'true' ">-RequireConsent</RequireConsentSwitch>
59+
<NonInteractiveSwitch Condition=" '$(VisualStudioVersion)' != '' AND '$(OS)' == 'Windows_NT' ">-NonInteractive</NonInteractiveSwitch>
60+
61+
<PaddedSolutionDir Condition=" '$(OS)' == 'Windows_NT'">"$(SolutionDir) "</PaddedSolutionDir>
62+
<PaddedSolutionDir Condition=" '$(OS)' != 'Windows_NT' ">"$(SolutionDir)"</PaddedSolutionDir>
63+
64+
<!-- Commands -->
65+
<RestoreCommand>$(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)" $(NonInteractiveSwitch) $(RequireConsentSwitch) -solutionDir $(PaddedSolutionDir)</RestoreCommand>
66+
<BuildCommand>$(NuGetCommand) pack "$(ProjectPath)" -Properties "Configuration=$(Configuration);Platform=$(Platform)" $(NonInteractiveSwitch) -OutputDirectory "$(PackageOutputDir)" -symbols</BuildCommand>
67+
68+
<!-- We need to ensure packages are restored prior to assembly resolve -->
69+
<BuildDependsOn Condition="$(RestorePackages) == 'true'">
70+
RestorePackages;
71+
$(BuildDependsOn);
72+
</BuildDependsOn>
73+
74+
<!-- Make the build depend on restore packages -->
75+
<BuildDependsOn Condition="$(BuildPackage) == 'true'">
76+
$(BuildDependsOn);
77+
BuildPackage;
78+
</BuildDependsOn>
79+
</PropertyGroup>
80+
81+
<Target Name="CheckPrerequisites">
82+
<!-- Raise an error if we're unable to locate nuget.exe -->
83+
<Error Condition="'$(DownloadNuGetExe)' != 'true' AND !Exists('$(NuGetExePath)')" Text="Unable to locate '$(NuGetExePath)'" />
84+
<!--
85+
Take advantage of MsBuild's build dependency tracking to make sure that we only ever download nuget.exe once.
86+
This effectively acts as a lock that makes sure that the download operation will only happen once and all
87+
parallel builds will have to wait for it to complete.
88+
-->
89+
<MsBuild Targets="_DownloadNuGet" Projects="$(MSBuildThisFileFullPath)" Properties="Configuration=NOT_IMPORTANT;DownloadNuGetExe=$(DownloadNuGetExe)" />
90+
</Target>
91+
92+
<Target Name="_DownloadNuGet">
93+
<DownloadNuGet OutputFilename="$(NuGetExePath)" Condition=" '$(DownloadNuGetExe)' == 'true' AND !Exists('$(NuGetExePath)')" />
94+
</Target>
95+
96+
<Target Name="RestorePackages" DependsOnTargets="CheckPrerequisites">
97+
<Exec Command="$(RestoreCommand)"
98+
Condition="'$(OS)' != 'Windows_NT' And Exists('$(PackagesConfig)')" />
99+
100+
<Exec Command="$(RestoreCommand)"
101+
LogStandardErrorAsError="true"
102+
Condition="'$(OS)' == 'Windows_NT' And Exists('$(PackagesConfig)')" />
103+
</Target>
104+
105+
<Target Name="BuildPackage" DependsOnTargets="CheckPrerequisites">
106+
<Exec Command="$(BuildCommand)"
107+
Condition=" '$(OS)' != 'Windows_NT' " />
108+
109+
<Exec Command="$(BuildCommand)"
110+
LogStandardErrorAsError="true"
111+
Condition=" '$(OS)' == 'Windows_NT' " />
112+
</Target>
113+
114+
<UsingTask TaskName="DownloadNuGet" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
115+
<ParameterGroup>
116+
<OutputFilename ParameterType="System.String" Required="true" />
117+
</ParameterGroup>
118+
<Task>
119+
<Reference Include="System.Core" />
120+
<Using Namespace="System" />
121+
<Using Namespace="System.IO" />
122+
<Using Namespace="System.Net" />
123+
<Using Namespace="Microsoft.Build.Framework" />
124+
<Using Namespace="Microsoft.Build.Utilities" />
125+
<Code Type="Fragment" Language="cs">
126+
<![CDATA[
127+
try {
128+
OutputFilename = Path.GetFullPath(OutputFilename);
129+
130+
Log.LogMessage("Downloading latest version of NuGet.exe...");
131+
WebClient webClient = new WebClient();
132+
webClient.DownloadFile("https://www.nuget.org/nuget.exe", OutputFilename);
133+
134+
return true;
135+
}
136+
catch (Exception ex) {
137+
Log.LogErrorFromException(ex);
138+
return false;
139+
}
140+
]]>
141+
</Code>
142+
</Task>
143+
</UsingTask>
144+
</Project>

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 James Holcomb
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
using NUnit.Framework;
2+
using FluentAssertions;
3+
using System.Reflection;
4+
using System.IO;
5+
using System.Diagnostics;
6+
using Newtonsoft.Json.Linq;
7+
using System;
8+
using Moq;
9+
using Microsoft.Build.Framework;
10+
using Newtonsoft.Json;
11+
using Newtonsoft.Json.Serialization;
12+
using System.Collections.Generic;
13+
using Microsoft.Build.Logging;
14+
using Microsoft.Build.Evaluation;
15+
using System.Linq;
16+
17+
namespace Postman.WebApi.MsBuildTask.Tests
18+
{
19+
[TestFixture]
20+
public class GeneratorTests
21+
{
22+
static int _folderCount = 2;
23+
static int _requestCount = 6;
24+
25+
[Test]
26+
public void creates_postman_collection()
27+
{
28+
// Arrange
29+
var assemblyFilePath = Assembly.GetExecutingAssembly().Location;
30+
31+
var generator = new CollectionGenerator();
32+
33+
// Act
34+
var actual = generator.Create(assemblyFilePath, Resources.DefaultEnvironmentKey, Resources.DefaultRouteTemplate);
35+
36+
// Assert
37+
var jsonSerializer = new JsonSerializer() { ContractResolver = new CamelCasePropertyNamesContractResolver() };
38+
Debug.WriteLine(JObject.FromObject(actual, jsonSerializer).ToString());
39+
40+
actual.Should().NotBeNull();
41+
actual.Requests.Count.Should().Be(_requestCount);
42+
actual.Folders.Count.Should().Be(_folderCount);
43+
actual.Description.Should().NotBeNull();
44+
}
45+
46+
[Test]
47+
public void executes_postman_generatetask()
48+
{
49+
// Arrange
50+
var mock = new Mock<IBuildEngine>();
51+
var task = new GenerateTask
52+
{
53+
AssemblyFilePath = Assembly.GetExecutingAssembly().Location,
54+
OutputDirectory = @".\",
55+
BuildEngine = mock.Object
56+
};
57+
58+
File.Delete(task.OutputFilePath);
59+
60+
// Act
61+
var actual = task.Execute();
62+
63+
// Assert
64+
actual.Should().BeTrue();
65+
File.Exists(task.OutputFilePath).Should().BeTrue();
66+
67+
AssertOutputFile(task.OutputFilePath);
68+
}
69+
70+
[Test]
71+
public void throws_filenotfoundexception_when_no_xml_doc_file_found()
72+
{
73+
// Arrange
74+
var mock = new Mock<IBuildEngine>();
75+
GenerateTask task = new GenerateTask
76+
{
77+
AssemblyFilePath = Assembly.GetExecutingAssembly().Location,
78+
OutputDirectory = @".\",
79+
BuildEngine = mock.Object
80+
};
81+
82+
var xmlFile = Assembly.GetExecutingAssembly().Location.Replace(Resources.DllFileExtension, Resources.XmlFileExtension);
83+
var tmpFile = @"tmp.xml";
84+
File.Move(xmlFile, tmpFile);
85+
86+
// Act/Assert
87+
Assert.That(() => task.Execute(), Throws.TypeOf<FileNotFoundException>());
88+
File.Move(tmpFile, xmlFile);
89+
}
90+
91+
[Test]
92+
public void throws_filenotfoundexception_when_no_assembly_file_found()
93+
{
94+
// Arrange
95+
var mock = new Mock<IBuildEngine>();
96+
GenerateTask task = new GenerateTask
97+
{
98+
AssemblyFilePath = "c:\badpath\file.dll",
99+
OutputDirectory = @".\",
100+
BuildEngine = mock.Object
101+
};
102+
103+
// Act/Assert
104+
Assert.That(() => task.Execute(), Throws.TypeOf<FileNotFoundException>());
105+
}
106+
107+
[Test]
108+
public void throws_argumentnullexception_when_assemblyfilepath_unspecified()
109+
{
110+
// Arrange
111+
var mock = new Mock<IBuildEngine>();
112+
GenerateTask task = new GenerateTask
113+
{
114+
BuildEngine = mock.Object
115+
};
116+
117+
// Act/Assert
118+
Assert.That(() => task.Execute(), Throws.TypeOf<ArgumentNullException>());
119+
}
120+
121+
[Test]
122+
public void executes_msbuild_postman_generatetask()
123+
{
124+
// arrange
125+
var loggers = new List<ILogger>();
126+
loggers.Add(new ConsoleLogger());
127+
128+
var projectCollection = new ProjectCollection();
129+
projectCollection.RegisterLoggers(loggers);
130+
131+
var projectDir = Path.GetDirectoryName(Path.GetDirectoryName(TestContext.CurrentContext.TestDirectory));
132+
var assemblyFile = Path.Combine(projectDir, @"bin\debug\Postman.WebApi.MsBuildTask.dll");
133+
var projectFile = Path.Combine(projectDir, @"Postman.WebApi.MsBuildTask.Tests.csproj");
134+
var task = @"Postman.WebApi.MsBuildTask.GenerateTask";
135+
136+
var project = projectCollection.LoadProject(projectFile);
137+
138+
project.Xml.AddUsingTask(task, assemblyFile, string.Empty);
139+
var target = project.Xml.AddTarget("AfterBuild");
140+
var el = target.AddTask(task);
141+
el.SetParameter("AssemblyFilePath", "$(TargetPath)");
142+
el.SetParameter("EnvironmentKey", "myhost");
143+
el.SetParameter("RouteTemplate", "myapi/v2/{controller}");
144+
el.SetParameter("OutputDirectory", Path.GetTempPath());
145+
146+
// act
147+
var actual = project.Build("AfterBuild");
148+
149+
// assert
150+
actual.Should().BeTrue();
151+
AssertOutputFile(Path.Combine(Path.GetTempPath(), @"Postman.WebApi.MsBuildTask.Tests.postman.json"));
152+
}
153+
154+
private void AssertOutputFile(string file)
155+
{
156+
File.Exists(file).Should().BeTrue();
157+
158+
var s = File.OpenText(file).ReadToEnd();
159+
160+
Debug.WriteLine(s);
161+
162+
var json = JObject.Parse(s);
163+
164+
json["folders"].Children().Count().Should().Be(_folderCount);
165+
json["requests"].Children().Count().Should().Be(_requestCount);
166+
}
167+
}
168+
}

0 commit comments

Comments
 (0)