Skip to content
Merged
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
9 changes: 9 additions & 0 deletions dotnet/Metaparticle.Package/Metaparticle.Tests/Config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace Metaparticle.Tests
{
public class Config : Attribute
{
public string[] Names { get;set; }
}
}
23 changes: 23 additions & 0 deletions dotnet/Metaparticle.Package/Metaparticle.Tests/DotnetTestRunner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.IO;
using static Metaparticle.Package.Util;

namespace Metaparticle.Tests
{
public class DotnetTestRunner : TestRunner
{
public bool Run(string[] tests)
{
foreach (var testFolder in tests)
{
Console.WriteLine($"Running Test {testFolder}");
var result = Exec("dotnet", $"test {testFolder}", Console.Out, Console.Error);

if (result.ExitCode != 0)
return false;
}

return true;
}
}
}
9 changes: 9 additions & 0 deletions dotnet/Metaparticle.Package/Metaparticle.Tests/TestRunner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.IO;

namespace Metaparticle.Tests
{
public interface TestRunner
{
bool Run(string[] tests);
}
}
25 changes: 23 additions & 2 deletions dotnet/Metaparticle.Package/Metaparticle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@
using System.IO;
using System.Text;
using dockerfile;
using Metaparticle.Tests;
using static Metaparticle.Package.Util;
using RuntimeConfig = Metaparticle.Runtime.Config;
using TestConfig = Metaparticle.Tests.Config;

namespace Metaparticle.Package
{
public class Driver
{
private Config config;
private RuntimeConfig runtimeConfig;
private TestConfig testConfig;

public Driver(Config config, RuntimeConfig runtimeConfig)
public Driver(Config config, RuntimeConfig runtimeConfig, TestConfig testConfig)
{
this.config = config;
this.runtimeConfig = runtimeConfig;
this.testConfig = testConfig;
}

private ImageBuilder getBuilder()
Expand Down Expand Up @@ -76,6 +80,8 @@ public void Build(string[] args)
TextWriter e = config.Quiet ? Console.Error : null;
if (procName == "dotnet")
{
RunTests();

dir = "bin/release/netcoreapp2.0/debian.8-x64/publish";
Exec("dotnet", "publish -r debian.8-x64 -c release", stdout: o, stderr: e);
//var dirInfo = new UnixDirectoryInfo(dir);
Expand Down Expand Up @@ -138,6 +144,15 @@ public void Build(string[] args)
exec.Logs(id, Console.Out, Console.Error);
}

private void RunTests()
{
var runTestsResult = new DotnetTestRunner().Run(testConfig.Names);
if (runTestsResult == false)
{
throw new Exception("Tests Failed.");
}
}

private string writeDockerfile(string dir, string exe, string[] args, Config config)
{
var dockerfilename = dir + "/Dockerfile";
Expand Down Expand Up @@ -188,6 +203,8 @@ public static void Containerize(string[] args, Action main)
}
Config config = new Config();
RuntimeConfig runtimeConfig = null;
TestConfig testConfig = null;

var trace = new StackTrace();
foreach (object attribute in trace.GetFrame(1).GetMethod().GetCustomAttributes(true))
{
Expand All @@ -199,8 +216,12 @@ public static void Containerize(string[] args, Action main)
{
runtimeConfig = (RuntimeConfig) attribute;
}
if (attribute is TestConfig)
{
testConfig = (TestConfig) attribute;
}
}
var mp = new Driver(config, runtimeConfig);
var mp = new Driver(config, runtimeConfig, testConfig);
mp.Build(args);
}
}
Expand Down
14 changes: 14 additions & 0 deletions dotnet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,19 @@ set METAPARTICLE_CONFIG_REPOSITORY=docker.io/myrepo/myimagename:sometag

This will set the `Repository` property that you would otherwise set in the attributes. See `Config.cs` for supported environment variable overrides.

### Tests
If you wish to add some test project to your metaparticle that get run as part of the build pipeline, you can add the tests projects (relative paths) to the `Tests.Config` attribute above `Main`, where you declare the runtime config.

```
[Metaparticle.Tests.Config(Names = new[] {"../my-test-folder1/", "../my-test-folder2/tests"})]
```

Sample project `simple-web` uses the test project `simple-test`. You can run the example to see it in action.

```
cd examples/simple
dotnet run
```

## Tutorial
For a more complete exploration of the Metaparticle/Package for .NET Core, please see the [in-depth tutorial](../tutorials/dotnet/tutorial.md).
23 changes: 23 additions & 0 deletions dotnet/examples/simple-test/SimpleTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using Xunit;
using simple;

namespace simpletest
{
public class SimpleTests
{
[Fact]
public void GivenTwoStrings_WhenConcatenateIsCalled_TwoStringsAreConcatenated()
{
// arrange
var stringOne = "Hello";
var stringTwo = "World";

// act
var result = Program.Concatenate(stringOne, stringTwo);

// assert
Assert.Equal("HelloWorld", result);
}
}
}
20 changes: 20 additions & 0 deletions dotnet/examples/simple-test/simple-test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\simple\simple.csproj" />
</ItemGroup>

</Project>
10 changes: 8 additions & 2 deletions dotnet/examples/simple/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@ public class Program
{
[Metaparticle.Runtime.Config]
[Metaparticle.Package.Config(Verbose = true,
Publish = false, Repository = "docker.io/docker-user-goes-here/dotnet-simple")]
Publish = false, Repository = "docker.io/docker-user-goes-here/dotnet-simple")]
[Metaparticle.Tests.Config(Names = new[] {"../simple-test"})]
public static void Main(string[] args) => Containerize (args, () =>
{
Console.Out.WriteLine(args);
int i = 0;
while (true) {
Console.WriteLine("Hello world " + (i++));
Console.WriteLine($"{Concatenate("Hello", " world")} " + (i++));
Thread.Sleep(10 * 1000);
}
});

public static string Concatenate(string one, string two)
{
return $"{one}{two}";
}
}
}