diff --git a/dotnet/Metaparticle.Package/Metaparticle.Tests/Config.cs b/dotnet/Metaparticle.Package/Metaparticle.Tests/Config.cs new file mode 100644 index 0000000..6db13af --- /dev/null +++ b/dotnet/Metaparticle.Package/Metaparticle.Tests/Config.cs @@ -0,0 +1,9 @@ +using System; + +namespace Metaparticle.Tests +{ + public class Config : Attribute + { + public string[] Names { get;set; } + } +} \ No newline at end of file diff --git a/dotnet/Metaparticle.Package/Metaparticle.Tests/DotnetTestRunner.cs b/dotnet/Metaparticle.Package/Metaparticle.Tests/DotnetTestRunner.cs new file mode 100644 index 0000000..d58619e --- /dev/null +++ b/dotnet/Metaparticle.Package/Metaparticle.Tests/DotnetTestRunner.cs @@ -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; + } + } +} \ No newline at end of file diff --git a/dotnet/Metaparticle.Package/Metaparticle.Tests/TestRunner.cs b/dotnet/Metaparticle.Package/Metaparticle.Tests/TestRunner.cs new file mode 100644 index 0000000..1992010 --- /dev/null +++ b/dotnet/Metaparticle.Package/Metaparticle.Tests/TestRunner.cs @@ -0,0 +1,9 @@ +using System.IO; + +namespace Metaparticle.Tests +{ + public interface TestRunner + { + bool Run(string[] tests); + } +} \ No newline at end of file diff --git a/dotnet/Metaparticle.Package/Metaparticle.cs b/dotnet/Metaparticle.Package/Metaparticle.cs index 403a43d..2a26bfd 100644 --- a/dotnet/Metaparticle.Package/Metaparticle.cs +++ b/dotnet/Metaparticle.Package/Metaparticle.cs @@ -4,8 +4,10 @@ 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 { @@ -13,11 +15,13 @@ 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() @@ -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); @@ -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"; @@ -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)) { @@ -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); } } diff --git a/dotnet/README.md b/dotnet/README.md index 44db008..5e281cf 100644 --- a/dotnet/README.md +++ b/dotnet/README.md @@ -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). diff --git a/dotnet/examples/simple-test/SimpleTest.cs b/dotnet/examples/simple-test/SimpleTest.cs new file mode 100644 index 0000000..55a1875 --- /dev/null +++ b/dotnet/examples/simple-test/SimpleTest.cs @@ -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); + } + } +} diff --git a/dotnet/examples/simple-test/simple-test.csproj b/dotnet/examples/simple-test/simple-test.csproj new file mode 100644 index 0000000..99061c3 --- /dev/null +++ b/dotnet/examples/simple-test/simple-test.csproj @@ -0,0 +1,20 @@ + + + + netcoreapp2.0 + + false + + + + + + + + + + + + + + diff --git a/dotnet/examples/simple/Program.cs b/dotnet/examples/simple/Program.cs index a71d560..9724481 100644 --- a/dotnet/examples/simple/Program.cs +++ b/dotnet/examples/simple/Program.cs @@ -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}"; + } } }