diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..15e9ba8 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +tests/install-id-fixtures/** binary diff --git a/README.md b/README.md index de8867c..70a90ae 100644 --- a/README.md +++ b/README.md @@ -216,6 +216,11 @@ project builds as netstandard2.0 with C# 7.3 so the vendoring floor stays honest. CI runs the suite on .NET 8 (Linux and Windows) and .NET Framework 4.8 (Windows). +The install-ID files in `tests/install-id-fixtures` are frozen once released: +`InstallIdFixturesTest` checks the loader still treats each one as +`expected.txt` records. Never edit or delete a fixture; a new case is a new +fixture plus its `expected.txt` line. + ## License MIT. diff --git a/tests/TraceClient.Tests/InstallIdFixturesTest.cs b/tests/TraceClient.Tests/InstallIdFixturesTest.cs new file mode 100644 index 0000000..9a4096f --- /dev/null +++ b/tests/TraceClient.Tests/InstallIdFixturesTest.cs @@ -0,0 +1,121 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using Xunit; + +namespace StephensonSoftware.Trace.Tests +{ + /// + /// Install-ID compatibility: the released loader's behaviour on every file in + /// tests/install-id-fixtures, as recorded in that directory's expected.txt. + /// An ID a previous release stored must keep loading as the same ID, and a file + /// the loader cannot read must never be overwritten. Released fixtures and their + /// loaded/kept lines are frozen; a new case is a new fixture plus its line. + /// + public sealed class InstallIdFixturesTest : IDisposable + { + private static readonly Regex Uuid4 = new Regex("^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"); + private static readonly string FixtureDirectory = SourceTree.PathTo("tests", "install-id-fixtures"); + + private readonly string _scratch = Path.Combine(Path.GetTempPath(), "trace-install-fixtures-" + Guid.NewGuid().ToString("N")); + + public void Dispose() + { + try + { + Directory.Delete(_scratch, true); + } + catch (Exception) + { + // nothing was made + } + } + + private static List ExpectedLines() + { + var lines = new List(); + foreach (string raw in File.ReadAllLines(Path.Combine(FixtureDirectory, "expected.txt"))) + { + string line = raw.Trim(); + if (line.Length == 0 || line.StartsWith("#", StringComparison.Ordinal)) + { + continue; + } + lines.Add(line.Split(new[] { ' ' }, 3)); + } + return lines; + } + + public static IEnumerable Cases() + { + return ExpectedLines().Select(parts => new object[] { parts[0], parts[1], parts.Length > 2 ? parts[2] : null }); + } + + private static bool Contains(byte[] haystack, byte[] needle) + { + for (int i = 0; i + needle.Length <= haystack.Length; i++) + { + int j = 0; + while (j < needle.Length && haystack[i + j] == needle[j]) + { + j++; + } + if (j == needle.Length) + { + return true; + } + } + return false; + } + + [Fact] + public void ExpectedTxt_NamesExactlyTheFixturesInTheDirectory() + { + var named = ExpectedLines().Select(parts => parts[0]).ToList(); + var present = Directory.GetFiles(FixtureDirectory, "*.id").Select(Path.GetFileName) + .OrderBy(name => name, StringComparer.Ordinal).ToList(); + + Assert.Equal(named.Count, named.Distinct().Count()); + Assert.Equal(present, named.OrderBy(name => name, StringComparer.Ordinal).ToList()); + Assert.All(ExpectedLines(), parts => + Assert.True((parts[1] == "loaded" && parts.Length == 3) || ((parts[1] == "replaced" || parts[1] == "kept") && parts.Length == 2), + string.Join(" ", parts))); + } + + [Theory] + [MemberData(nameof(Cases))] + public void InstallIdFromFile_DoesWhatTheReleasedLoaderDid(string fixture, string outcome, string id) + { + string directory = Path.Combine(_scratch, Guid.NewGuid().ToString("N")); + Directory.CreateDirectory(directory); + string path = Path.Combine(directory, fixture); + File.Copy(Path.Combine(FixtureDirectory, fixture), path); + byte[] before = File.ReadAllBytes(path); + + string returned = TraceClient.InstallIdFromFile(path); + byte[] after = File.ReadAllBytes(path); + + switch (outcome) + { + case "loaded": + Assert.Equal(id, returned); + Assert.Equal(before, after); + break; + case "replaced": + Assert.Matches(Uuid4, returned); + Assert.Equal(Encoding.ASCII.GetBytes(returned + "\n"), after); + break; + case "kept": + Assert.Equal(before, after); + Assert.Matches(Uuid4, returned); + Assert.False(Contains(before, Encoding.ASCII.GetBytes(returned)), returned + " is in " + fixture); + break; + default: + throw new InvalidOperationException("unknown outcome in expected.txt: " + outcome); + } + } + } +} diff --git a/tests/TraceClient.Tests/SourceTree.cs b/tests/TraceClient.Tests/SourceTree.cs new file mode 100644 index 0000000..c4d2c5e --- /dev/null +++ b/tests/TraceClient.Tests/SourceTree.cs @@ -0,0 +1,38 @@ +using System; +using System.IO; + +namespace StephensonSoftware.Trace.Tests +{ + /// + /// Files the suite reads from the repository itself, found by walking up from + /// the test assembly's directory (bin/Release/<framework> on every OS) to the + /// directory that holds TraceClient.sln. + /// + internal static class SourceTree + { + public static string Root + { + get + { + for (DirectoryInfo dir = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory); dir != null; dir = dir.Parent) + { + if (File.Exists(Path.Combine(dir.FullName, "TraceClient.sln"))) + { + return dir.FullName; + } + } + throw new InvalidOperationException("TraceClient.sln not found above " + AppDomain.CurrentDomain.BaseDirectory); + } + } + + public static string PathTo(params string[] parts) + { + string path = Root; + foreach (string part in parts) + { + path = Path.Combine(path, part); + } + return path; + } + } +} diff --git a/tests/TraceClient.Tests/VersionConsistencyTest.cs b/tests/TraceClient.Tests/VersionConsistencyTest.cs new file mode 100644 index 0000000..c6e8efc --- /dev/null +++ b/tests/TraceClient.Tests/VersionConsistencyTest.cs @@ -0,0 +1,63 @@ +using System.IO; +using System.Linq; +using System.Text.RegularExpressions; +using Xunit; + +namespace StephensonSoftware.Trace.Tests +{ + /// + /// Every place the client's version is written agrees with the build file, + /// TraceClient.csproj's <Version>, and each appears exactly once. + /// + public sealed class VersionConsistencyTest + { + private static string Read(params string[] parts) + { + return File.ReadAllText(SourceTree.PathTo(parts)); + } + + private static string BuildVersion() + { + MatchCollection found = Regex.Matches(Read("src", "TraceClient", "TraceClient.csproj"), "([^<]+)"); + return Assert.Single(found.Cast()).Groups[1].Value; + } + + private static void AppearsOnce(string text, string expected, string where) + { + int count = Regex.Matches(text, Regex.Escape(expected)).Count; + Assert.True(count == 1, "expected \"" + expected + "\" exactly once in " + where + ", found " + count); + } + + [Fact] + public void EveryVersionSpotMatchesTheCsproj() + { + string version = BuildVersion(); + string source = Read("src", "TraceClient", "TraceClient.cs"); + + AppearsOnce(source, "trace-client " + version + " --", "TraceClient.cs header"); + AppearsOnce(source, "const string Version = \"" + version + "\"", "TraceClient.cs"); + AppearsOnce(Read("README.md"), "trace-client/" + version + " (", "README.md"); + Assert.Equal(TraceClient.Version, version); + } + + [Fact] + public void NoSpotNamesADifferentVersion() + { + string version = BuildVersion(); + string source = Read("src", "TraceClient", "TraceClient.cs"); + + foreach (Match m in Regex.Matches(source, "trace-client (\\S+) --")) + { + Assert.Equal(version, m.Groups[1].Value); + } + foreach (Match m in Regex.Matches(source, "const string Version = \"([^\"]*)\"")) + { + Assert.Equal(version, m.Groups[1].Value); + } + foreach (Match m in Regex.Matches(Read("README.md"), "trace-client/(\\S+) \\(")) + { + Assert.Equal(version, m.Groups[1].Value); + } + } + } +} diff --git a/tests/install-id-fixtures/bad-chars.id b/tests/install-id-fixtures/bad-chars.id new file mode 100644 index 0000000..71c1170 --- /dev/null +++ b/tests/install-id-fixtures/bad-chars.id @@ -0,0 +1 @@ +not an id! diff --git a/tests/install-id-fixtures/bom.id b/tests/install-id-fixtures/bom.id new file mode 100644 index 0000000..cfa81a3 --- /dev/null +++ b/tests/install-id-fixtures/bom.id @@ -0,0 +1 @@ +bom-id diff --git a/tests/install-id-fixtures/comment-first.id b/tests/install-id-fixtures/comment-first.id new file mode 100644 index 0000000..a87026e --- /dev/null +++ b/tests/install-id-fixtures/comment-first.id @@ -0,0 +1,2 @@ +# install id +real-id diff --git a/tests/install-id-fixtures/created.id b/tests/install-id-fixtures/created.id new file mode 100644 index 0000000..dadc3db --- /dev/null +++ b/tests/install-id-fixtures/created.id @@ -0,0 +1 @@ +3f2b8c1e-7a4d-4e6f-9b2a-5c8d1e0f7a6b diff --git a/tests/install-id-fixtures/crlf.id b/tests/install-id-fixtures/crlf.id new file mode 100644 index 0000000..264c957 --- /dev/null +++ b/tests/install-id-fixtures/crlf.id @@ -0,0 +1 @@ +crlf-id diff --git a/tests/install-id-fixtures/empty.id b/tests/install-id-fixtures/empty.id new file mode 100644 index 0000000..e69de29 diff --git a/tests/install-id-fixtures/expected.txt b/tests/install-id-fixtures/expected.txt new file mode 100644 index 0000000..c085b03 --- /dev/null +++ b/tests/install-id-fixtures/expected.txt @@ -0,0 +1,27 @@ +# Install-ID fixtures: what TraceClient.InstallIdFromFile does with each *.id file in this directory, +# as measured against the released client. One line per fixture, sorted by file name: +# loaded the loader returns exactly and leaves the file untouched. +# replaced the file holds no ID, so the loader writes a fresh lowercase uuid4 plus "\n" and returns it. +# kept the file cannot be read, so the loader leaves it byte-identical and returns a fresh uuid4 +# for this process only. +# Lines marked loaded or kept that a release shipped must never change; a new case is a new fixture plus its line. +bad-chars.id replaced +bom.id loaded bom-id +comment-first.id loaded real-id +created.id loaded 3f2b8c1e-7a4d-4e6f-9b2a-5c8d1e0f7a6b +crlf.id loaded crlf-id +empty.id replaced +first-of-two.id loaded first-id +id-then-not-utf8.id loaded good-id +invalid-lines-first.id loaded third.is-valid +leading-blank-lines.id loaded after-blanks +max-length.id loaded aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +non-ascii.id replaced +not-utf8.id replaced +padded.id loaded padded-id +plain.id loaded plain-id +tabs.id loaded tabbed_id +too-long.id replaced +trailing-newline.id loaded with.newline_1 +uppercase-uuid.id loaded 0F8FAD5B-D9CB-469F-A165-70867728950E +whitespace-only.id replaced diff --git a/tests/install-id-fixtures/first-of-two.id b/tests/install-id-fixtures/first-of-two.id new file mode 100644 index 0000000..12b50c2 --- /dev/null +++ b/tests/install-id-fixtures/first-of-two.id @@ -0,0 +1,2 @@ +first-id +second-id diff --git a/tests/install-id-fixtures/id-then-not-utf8.id b/tests/install-id-fixtures/id-then-not-utf8.id new file mode 100644 index 0000000..5e6253f --- /dev/null +++ b/tests/install-id-fixtures/id-then-not-utf8.id @@ -0,0 +1,2 @@ +good-id +ÿþ diff --git a/tests/install-id-fixtures/invalid-lines-first.id b/tests/install-id-fixtures/invalid-lines-first.id new file mode 100644 index 0000000..9b883c9 --- /dev/null +++ b/tests/install-id-fixtures/invalid-lines-first.id @@ -0,0 +1,4 @@ +has space +not/valid +bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +third.is-valid diff --git a/tests/install-id-fixtures/leading-blank-lines.id b/tests/install-id-fixtures/leading-blank-lines.id new file mode 100644 index 0000000..f96b0dc --- /dev/null +++ b/tests/install-id-fixtures/leading-blank-lines.id @@ -0,0 +1,5 @@ + + + + +after-blanks diff --git a/tests/install-id-fixtures/max-length.id b/tests/install-id-fixtures/max-length.id new file mode 100644 index 0000000..f040e66 --- /dev/null +++ b/tests/install-id-fixtures/max-length.id @@ -0,0 +1 @@ +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa diff --git a/tests/install-id-fixtures/non-ascii.id b/tests/install-id-fixtures/non-ascii.id new file mode 100644 index 0000000..c9cf8fd --- /dev/null +++ b/tests/install-id-fixtures/non-ascii.id @@ -0,0 +1 @@ +café-id diff --git a/tests/install-id-fixtures/not-utf8.id b/tests/install-id-fixtures/not-utf8.id new file mode 100644 index 0000000..8c73662 Binary files /dev/null and b/tests/install-id-fixtures/not-utf8.id differ diff --git a/tests/install-id-fixtures/padded.id b/tests/install-id-fixtures/padded.id new file mode 100644 index 0000000..7180291 --- /dev/null +++ b/tests/install-id-fixtures/padded.id @@ -0,0 +1 @@ + padded-id diff --git a/tests/install-id-fixtures/plain.id b/tests/install-id-fixtures/plain.id new file mode 100644 index 0000000..08c035b --- /dev/null +++ b/tests/install-id-fixtures/plain.id @@ -0,0 +1 @@ +plain-id \ No newline at end of file diff --git a/tests/install-id-fixtures/tabs.id b/tests/install-id-fixtures/tabs.id new file mode 100644 index 0000000..b8885ba --- /dev/null +++ b/tests/install-id-fixtures/tabs.id @@ -0,0 +1 @@ + tabbed_id diff --git a/tests/install-id-fixtures/too-long.id b/tests/install-id-fixtures/too-long.id new file mode 100644 index 0000000..442b421 --- /dev/null +++ b/tests/install-id-fixtures/too-long.id @@ -0,0 +1 @@ +cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc diff --git a/tests/install-id-fixtures/trailing-newline.id b/tests/install-id-fixtures/trailing-newline.id new file mode 100644 index 0000000..60a4d0b --- /dev/null +++ b/tests/install-id-fixtures/trailing-newline.id @@ -0,0 +1 @@ +with.newline_1 diff --git a/tests/install-id-fixtures/uppercase-uuid.id b/tests/install-id-fixtures/uppercase-uuid.id new file mode 100644 index 0000000..37f9243 --- /dev/null +++ b/tests/install-id-fixtures/uppercase-uuid.id @@ -0,0 +1 @@ +0F8FAD5B-D9CB-469F-A165-70867728950E diff --git a/tests/install-id-fixtures/whitespace-only.id b/tests/install-id-fixtures/whitespace-only.id new file mode 100644 index 0000000..58a592e --- /dev/null +++ b/tests/install-id-fixtures/whitespace-only.id @@ -0,0 +1,2 @@ + +