|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.IO; |
| 5 | +using System.Linq; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace Test |
| 9 | +{ |
| 10 | + public class DotNetFixture : IDisposable |
| 11 | + { |
| 12 | + private static string RelativeTemplatePath = @"../../../../content/dotnet-template-azure-iot-edge-module/CSharp/"; |
| 13 | + public DotNetFixture() |
| 14 | + { |
| 15 | + Process.Start("dotnet.exe", "new -i " + RelativeTemplatePath).WaitForExit(); |
| 16 | + } |
| 17 | + |
| 18 | + public void Dispose() |
| 19 | + { |
| 20 | + Console.WriteLine("DotNetFixture: Disposing DotnetFixture"); |
| 21 | + |
| 22 | + // uninstall does not work now according to dotnet issue |
| 23 | + // issue link: https://github.com/dotnet/templating/issues/1226 |
| 24 | + Process.Start("dotnet.exe", "new -u " + RelativeTemplatePath).WaitForExit(); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + public class Test : IClassFixture<DotNetFixture> |
| 29 | + { |
| 30 | + private DotNetFixture fixture; |
| 31 | + private const string TargetAll = "all"; |
| 32 | + private const string TargetDeploy = "deploy"; |
| 33 | + private const string ArchLinux64 = "linux64"; |
| 34 | + private const string ArchWindowsNano = "windowsNano"; |
| 35 | + |
| 36 | + public Test(DotNetFixture fixture) |
| 37 | + { |
| 38 | + this.fixture = fixture; |
| 39 | + } |
| 40 | + |
| 41 | + public static Dictionary<string, List<string>> FlagFilesMapping = new Dictionary<string, List<string>>{ |
| 42 | + { |
| 43 | + TargetAll, new List<string>() |
| 44 | + }, |
| 45 | + { |
| 46 | + TargetDeploy, new List<string> { |
| 47 | + "deployment.json" |
| 48 | + } |
| 49 | + }, |
| 50 | + { |
| 51 | + ArchLinux64, new List<string> { |
| 52 | + "Docker/linux-x64/Dockerfile", |
| 53 | + "Docker/linux-x64/Dockerfile.debug" |
| 54 | + } |
| 55 | + }, |
| 56 | + { |
| 57 | + ArchWindowsNano, new List<string> { |
| 58 | + "Docker/windows-nano/Dockerfile" |
| 59 | + } |
| 60 | + } |
| 61 | + }; |
| 62 | + |
| 63 | + private static string BeforeEach(string target = TargetAll, bool linux64 = true, bool windowsNano = true, bool skipRestore = false) |
| 64 | + { |
| 65 | + var scaffoldName = Path.GetRandomFileName().Replace(".", "").ToString(); |
| 66 | + FlagFilesMapping[TargetAll] = new List<string> {scaffoldName + ".csproj", "Program.cs", ".gitignore"}; |
| 67 | + var command = "new aziotedgemodule -n " + scaffoldName + " -t " + target + " -lx " + linux64 + " -wn " + windowsNano + " -s " + skipRestore; |
| 68 | + Process.Start("dotnet.exe", command).WaitForExit(); |
| 69 | + return scaffoldName; |
| 70 | + } |
| 71 | + |
| 72 | + [Theory] |
| 73 | + [InlineData(TargetAll, true, true, true)] |
| 74 | + [InlineData(TargetAll, true, true, false)] |
| 75 | + [InlineData(TargetAll, true, false, true)] |
| 76 | + [InlineData(TargetAll, true, false, false)] |
| 77 | + [InlineData(TargetAll, false, true, true)] |
| 78 | + [InlineData(TargetAll, false, true, false)] |
| 79 | + [InlineData(TargetAll, false, false, true)] |
| 80 | + [InlineData(TargetAll, false, false, false)] |
| 81 | + [InlineData(TargetDeploy, true, true, true)] |
| 82 | + [InlineData(TargetDeploy, true, true, false)] |
| 83 | + [InlineData(TargetDeploy, true, false, true)] |
| 84 | + [InlineData(TargetDeploy, true, false, false)] |
| 85 | + [InlineData(TargetDeploy, false, true, true)] |
| 86 | + [InlineData(TargetDeploy, false, true, false)] |
| 87 | + [InlineData(TargetDeploy, false, false, true)] |
| 88 | + [InlineData(TargetDeploy, false, false, false)] |
| 89 | + public void TestArchitecture(string target, bool linux64, bool windowsNano, bool skipRestore) |
| 90 | + { |
| 91 | + var scaffoldName = BeforeEach(target, linux64, windowsNano, skipRestore); |
| 92 | + var filesToCheck = new List<string>(); |
| 93 | + if(target == TargetDeploy) |
| 94 | + { |
| 95 | + filesToCheck = FlagFilesMapping[TargetDeploy]; |
| 96 | + } |
| 97 | + else if (target == TargetAll) |
| 98 | + { |
| 99 | + if(skipRestore) |
| 100 | + { |
| 101 | + Assert.True(!Directory.Exists(Path.Combine(scaffoldName, "obj"))); |
| 102 | + } |
| 103 | + else |
| 104 | + { |
| 105 | + Assert.True(Directory.Exists(Path.Combine(scaffoldName, "obj"))); |
| 106 | + } |
| 107 | + |
| 108 | + filesToCheck = FlagFilesMapping[TargetDeploy].Union(FlagFilesMapping[TargetAll]).ToList(); |
| 109 | + |
| 110 | + if (linux64) |
| 111 | + { |
| 112 | + filesToCheck.AddRange(FlagFilesMapping[ArchLinux64]); |
| 113 | + |
| 114 | + } |
| 115 | + if (windowsNano) |
| 116 | + { |
| 117 | + filesToCheck.AddRange(FlagFilesMapping[ArchWindowsNano]); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + foreach (var file in filesToCheck) |
| 122 | + { |
| 123 | + Assert.True(File.Exists(Path.Combine(scaffoldName, file))); |
| 124 | + } |
| 125 | + Directory.Delete(scaffoldName, true); |
| 126 | + } |
| 127 | + |
| 128 | + [Fact] |
| 129 | + public void TestDeployUnnecessaryFiles() |
| 130 | + { |
| 131 | + var scaffoldName = BeforeEach(TargetDeploy); |
| 132 | + var filesExistsToCheck = FlagFilesMapping[TargetDeploy]; |
| 133 | + var filesNonExistsToCheck = FlagFilesMapping[ArchLinux64].Union(FlagFilesMapping[ArchWindowsNano]).Union(FlagFilesMapping[TargetAll]); |
| 134 | + |
| 135 | + foreach (var file in filesExistsToCheck) |
| 136 | + { |
| 137 | + Assert.True(File.Exists(Path.Combine(scaffoldName, file))); |
| 138 | + } |
| 139 | + foreach (var file in filesNonExistsToCheck) |
| 140 | + { |
| 141 | + Assert.True(!File.Exists(Path.Combine(scaffoldName, file))); |
| 142 | + } |
| 143 | + Assert.True(!Directory.Exists(Path.Combine(scaffoldName, "obj"))); |
| 144 | + Directory.Delete(scaffoldName, true); |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments