Skip to content

Commit 64f02a5

Browse files
sblomAndrew Gibson
authored andcommitted
Added tests for Dockerfile
1 parent cd7ed04 commit 64f02a5

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

test/json-ld.net.tests/DockerTests.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel;
4+
using System.Diagnostics;
5+
using System.IO;
6+
using System.Linq.Expressions;
7+
using System.Text;
8+
using Xunit;
9+
using Xunit.Abstractions;
10+
using Xunit.Sdk;
11+
12+
namespace JsonLD.Test
13+
{
14+
public class DockerTests
15+
{
16+
private readonly ITestOutputHelper output;
17+
18+
public DockerTests(ITestOutputHelper output)
19+
{
20+
this.output = output;
21+
}
22+
23+
[SkippableFact]
24+
public void DockerBuild()
25+
{
26+
try
27+
{
28+
var exit = ExecDocker("build -t json-ld.net .");
29+
Assert.Equal(0, exit);
30+
}
31+
catch (Win32Exception ex) when (ex.Message == "The system cannot find the file specified")
32+
{
33+
Skip.If(true, "Couldn't find docker in your path.\nThis is only required for building/testing inside a docker container.");
34+
}
35+
}
36+
37+
[SkippableFact]
38+
public void DockerTest()
39+
{
40+
try
41+
{
42+
// --filter option required to avoid going full inception and
43+
// testing a container inside a container inside a conta...
44+
var exit = ExecDocker($"run --rm json-ld.net dotnet test --filter FullyQualifiedName!~{nameof(DockerTests)}");
45+
Assert.Equal(0, exit);
46+
}
47+
catch (Win32Exception ex) when (ex.Message == "The system cannot find the file specified")
48+
{
49+
Skip.If(true, "Couldn't find docker in your path.\nThis is only required for building/testing inside a docker container.");
50+
}
51+
}
52+
53+
private int ExecDocker(string args)
54+
{
55+
var workspace = Environment.GetEnvironmentVariable("GITHUB_WORKSPACE");
56+
if (string.IsNullOrEmpty(workspace))
57+
{
58+
// Current directory for test context is usually REPO_HOME/test/json-ld.net.tests/bin/Release/netcoreapp2.1,
59+
// and we want just REPO_HOME.
60+
workspace = Path.Combine(Directory.GetCurrentDirectory(), "../../../../..");
61+
}
62+
63+
var process = Process.Start(new ProcessStartInfo
64+
{
65+
UseShellExecute = false,
66+
RedirectStandardOutput = true,
67+
RedirectStandardError = true,
68+
WorkingDirectory = workspace,
69+
FileName = "docker",
70+
Arguments = args
71+
});
72+
73+
process.WaitForExit();
74+
75+
output.WriteLine("=======STDOUT=======");
76+
output.WriteLine(process.StandardOutput.ReadToEnd());
77+
output.WriteLine("\n\n\n\n=======STDERR=======");
78+
output.WriteLine(process.StandardError.ReadToEnd());
79+
80+
return process.ExitCode;
81+
}
82+
}
83+
}

test/json-ld.net.tests/json-ld.net.tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
88
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
99
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
10+
<RootNamespace>JsonLD.Test</RootNamespace>
1011
</PropertyGroup>
1112

1213
<ItemGroup>
@@ -24,6 +25,7 @@
2425
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
2526
<PackageReference Include="xunit" Version="2.4.1" />
2627
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
28+
<PackageReference Include="Xunit.SkippableFact" Version="1.4.13" />
2729
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
2830
</ItemGroup>
2931

0 commit comments

Comments
 (0)