-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathTestsBase.cs
119 lines (118 loc) · 4.84 KB
/
TestsBase.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text.RegularExpressions;
using WebCompiler.Compile;
namespace Tests_WebCompiler
{
public class TestsBase
{
protected Func<string, CompilationStep> pipeline;
protected string input;
protected List<string> input_files;
protected List<string> output_files;
protected List<string> unexpected_files;
protected List<string> temporary_files;
protected string expected_output;
[TearDown]
protected void DeleteTestCreatedFiles()
{
DeleteTemporaryFiles();
DeleteOutputFiles();
var generated_files = output_files?.Concat(temporary_files ?? Enumerable.Empty<string>()).ToList() ?? new List<string>();
var dirs = generated_files.Select(Path.GetDirectoryName).Select(Path.GetFullPath).ToList();
var input_dirs = (input_files ?? Enumerable.Empty<string>()).Append(input).Select(Path.GetDirectoryName).Where(d => d != null).Select(Path.GetFullPath).ToList();
foreach (var dir in dirs.OrderByDescending(d => d.Count(c => c == '\\' || c == '/')))
{
if (input_dirs.Any(dir.StartsWith))
{
continue;
}
if (Directory.Exists(dir))
{
Directory.Delete(dir);
}
}
}
protected void DeleteOutputFiles()
{
if (output_files == null)
{
return;
}
foreach (var file in output_files)
{
if (File.Exists(file))
{
File.Delete(file);
}
}
}
protected void DeleteTemporaryFiles()
{
if (temporary_files == null)
{
return;
}
foreach (var tmp_file in temporary_files)
{
if (File.Exists(tmp_file))
{
File.Delete(tmp_file);
}
}
}
protected void Test()
{
var timestamp = ProcessFile();
var new_timestamp = ProcessFile();
Assert.That(timestamp, Is.EqualTo(new_timestamp), "Compiling a second time should not alter the file");
TestEqual(expected_output, output_files.Last());
}
protected DateTime ProcessFile()
{
CompilationStep result = null;
Assert.DoesNotThrow(() => result = pipeline(input), "Compiling should not result in exception");
Assert.That(result, Is.Not.Null, "Compilation result may not be null");
Assert.That(result.Errors == null || !result.Errors.Any(), Is.True, "Compilation should not result in error");
Assert.That(Path.GetFullPath(output_files.Last()), Is.EqualTo(Path.GetFullPath(result.OutputFile)), "Unexpected output file");
foreach (var file in output_files)
{
Assert.That(File.Exists(file), $"Output file or intermediate file {file} should exist");
}
if (unexpected_files != null)
{
foreach (var file in unexpected_files)
{
Assert.That(File.Exists(file), Is.False, $"File {file} should NOT exist, but does");
}
}
return new FileInfo(output_files.Last()).LastWriteTimeUtc;
}
private static string Decompressed(string file)
{
using var source_stream = File.OpenRead(file);
using var gzip_stream = new GZipStream(source_stream, CompressionMode.Decompress);
var stream_reader = new StreamReader(gzip_stream);
return stream_reader.ReadToEnd();
}
protected void TestEqual(string expected, string value)
{
if (expected.EndsWith("gz"))
{
var normalisedExpected = Regex.Replace(Decompressed(expected), @"\r\n?|\n", string.Empty).Replace('\\', '/');
var normalisedActual = Regex.Replace(Decompressed(value), @"\r\n?|\n", string.Empty).Replace('\\', '/');
Assert.That(normalisedExpected, Is.EqualTo(normalisedActual), "Compressed files should have the same content when decompressed");
}
else
{
var normalisedExpected = Regex.Replace(File.ReadAllText(expected, Compiler.Encoding), @"\r\n?|\n", string.Empty).Replace('\\', '/');
var normalisedActual = Regex.Replace(File.ReadAllText(value, Compiler.Encoding), @"\r\n?|\n", string.Empty).Replace('\\', '/');
Assert.That(normalisedExpected, Is.EqualTo(normalisedActual), "Files should be identical");
}
}
}
}