Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tests/install-id-fixtures/** binary
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
121 changes: 121 additions & 0 deletions tests/TraceClient.Tests/InstallIdFixturesTest.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// 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.
/// </summary>
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<string[]> ExpectedLines()
{
var lines = new List<string[]>();
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<object[]> 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);
}
}
}
}
38 changes: 38 additions & 0 deletions tests/TraceClient.Tests/SourceTree.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.IO;

namespace StephensonSoftware.Trace.Tests
{
/// <summary>
/// Files the suite reads from the repository itself, found by walking up from
/// the test assembly's directory (bin/Release/&lt;framework&gt; on every OS) to the
/// directory that holds TraceClient.sln.
/// </summary>
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;
}
}
}
63 changes: 63 additions & 0 deletions tests/TraceClient.Tests/VersionConsistencyTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using Xunit;

namespace StephensonSoftware.Trace.Tests
{
/// <summary>
/// Every place the client's version is written agrees with the build file,
/// TraceClient.csproj's &lt;Version&gt;, and each appears exactly once.
/// </summary>
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"), "<Version>([^<]+)</Version>");
return Assert.Single(found.Cast<Match>()).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);
}
}
}
}
1 change: 1 addition & 0 deletions tests/install-id-fixtures/bad-chars.id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
not an id!
1 change: 1 addition & 0 deletions tests/install-id-fixtures/bom.id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bom-id
2 changes: 2 additions & 0 deletions tests/install-id-fixtures/comment-first.id
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# install id
real-id
1 change: 1 addition & 0 deletions tests/install-id-fixtures/created.id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3f2b8c1e-7a4d-4e6f-9b2a-5c8d1e0f7a6b
1 change: 1 addition & 0 deletions tests/install-id-fixtures/crlf.id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
crlf-id
Empty file.
27 changes: 27 additions & 0 deletions tests/install-id-fixtures/expected.txt
Original file line number Diff line number Diff line change
@@ -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:
# <file> loaded <id> the loader returns exactly <id> and leaves the file untouched.
# <file> replaced the file holds no ID, so the loader writes a fresh lowercase uuid4 plus "\n" and returns it.
# <file> 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
2 changes: 2 additions & 0 deletions tests/install-id-fixtures/first-of-two.id
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
first-id
second-id
2 changes: 2 additions & 0 deletions tests/install-id-fixtures/id-then-not-utf8.id
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
good-id
ÿþ
4 changes: 4 additions & 0 deletions tests/install-id-fixtures/invalid-lines-first.id
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
has space
not/valid
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
third.is-valid
5 changes: 5 additions & 0 deletions tests/install-id-fixtures/leading-blank-lines.id
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@




after-blanks
1 change: 1 addition & 0 deletions tests/install-id-fixtures/max-length.id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
1 change: 1 addition & 0 deletions tests/install-id-fixtures/non-ascii.id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
café-id
Binary file added tests/install-id-fixtures/not-utf8.id
Binary file not shown.
1 change: 1 addition & 0 deletions tests/install-id-fixtures/padded.id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
padded-id
1 change: 1 addition & 0 deletions tests/install-id-fixtures/plain.id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
plain-id
1 change: 1 addition & 0 deletions tests/install-id-fixtures/tabs.id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tabbed_id
1 change: 1 addition & 0 deletions tests/install-id-fixtures/too-long.id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
1 change: 1 addition & 0 deletions tests/install-id-fixtures/trailing-newline.id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
with.newline_1
1 change: 1 addition & 0 deletions tests/install-id-fixtures/uppercase-uuid.id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0F8FAD5B-D9CB-469F-A165-70867728950E
2 changes: 2 additions & 0 deletions tests/install-id-fixtures/whitespace-only.id
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@


Loading