diff --git a/src/Nullean.Entities.CsCheck.FileSystem/FileWorld.cs b/src/Nullean.Entities.CsCheck.FileSystem/FileWorld.cs
index be5e330..bef8cc8 100644
--- a/src/Nullean.Entities.CsCheck.FileSystem/FileWorld.cs
+++ b/src/Nullean.Entities.CsCheck.FileSystem/FileWorld.cs
@@ -76,7 +76,7 @@ public FileWorld() : this(new MockFileSystem(), 0)
/// A world over a file system a baseline value already has; names start high so they cannot replace the baseline's own files.
public FileWorld(MockFileSystem fileSystem, int firstName = 10_000)
{
- _counter = Counters.GetValue(fileSystem, _ => new StrongBox(firstName));
+ _counter = Counters.GetValue(fileSystem, _ => new StrongBox(firstName - 1));
FileSystem = fileSystem;
Root = OperatingSystem.IsWindows() ? @"C:\docs" : "/docs";
Outside = OperatingSystem.IsWindows() ? @"C:\elsewhere" : "/elsewhere";
@@ -92,7 +92,7 @@ public FileWorld(MockFileSystem fileSystem, int firstName = 10_000)
private string Combine(params string[] parts) => FileSystem.Path.Combine(parts);
- private string NextName(PCG pcg) => $"{Words[(int)pcg.Next((uint)Words.Length)]}-{_counter.Value++}";
+ private string NextName(PCG pcg) => $"{Words[(int)pcg.Next((uint)Words.Length)]}-{Interlocked.Increment(ref _counter.Value)}";
private string Folder(PCG pcg)
{
@@ -100,7 +100,16 @@ private string Folder(PCG pcg)
return folder.Length == 0 ? Root : Combine(Root, folder.Replace('/', FileSystem.Path.DirectorySeparatorChar));
}
+ // Axes of one test run at the same time over a constant base, so its file system is shared: each draw changes it under a lock on it.
public IFileInfo File(PCG pcg, FileKind kind)
+ {
+ lock (FileSystem)
+ {
+ return FileCore(pcg, kind);
+ }
+ }
+
+ private IFileInfo FileCore(PCG pcg, FileKind kind)
{
var name = NextName(pcg);
switch (kind)
@@ -172,6 +181,14 @@ public IFileInfo File(PCG pcg, FileKind kind)
}
public IDirectoryInfo Directory(PCG pcg, DirectoryKind kind)
+ {
+ lock (FileSystem)
+ {
+ return DirectoryCore(pcg, kind);
+ }
+ }
+
+ private IDirectoryInfo DirectoryCore(PCG pcg, DirectoryKind kind)
{
switch (kind)
{
diff --git a/src/Nullean.Entities.CsCheck.FileSystem/README.md b/src/Nullean.Entities.CsCheck.FileSystem/README.md
index 9d44879..37285e3 100644
--- a/src/Nullean.Entities.CsCheck.FileSystem/README.md
+++ b/src/Nullean.Entities.CsCheck.FileSystem/README.md
@@ -86,6 +86,7 @@ where `valid` is one `Docset` whose files all satisfy the rules (for example, a
- The root is `/docs` (`C:\docs` on Windows) and the outside folder is `/elsewhere` (`C:\elsewhere`). A rule that depends on other paths needs a hand-built baseline.
- `MockFileSystem` is in-memory, so rules about permissions, locks or real disk behaviour are not exercised.
- Rules whose counter-examples need a structure the generator cannot draw (for example a specific folder name your rule looks for) still need one written by hand.
+- A `Gen.Const` base is one `MockFileSystem` shared by every axis, and axes run at the same time. A counter-example given to `AutoTest` runs once per base, under a lock on it, and later axes reuse the result; the file system is not cloned, so a counter-example should add files rather than edit the base's own.
- It reaches the file-system members through AutoGen's reflection; do not reference it from an AOT-published app.
## Learn more
diff --git a/src/Nullean.Entities.CsCheck/AutoTest.cs b/src/Nullean.Entities.CsCheck/AutoTest.cs
index f076f35..2f22755 100644
--- a/src/Nullean.Entities.CsCheck/AutoTest.cs
+++ b/src/Nullean.Entities.CsCheck/AutoTest.cs
@@ -7,6 +7,7 @@
// exposes each axis as a named method. Report() runs every axis and records what each did; Run() does the same and throws when any failed; Cases lists the
// axes the type actually declares, one per test, for a test framework's data source. An axis the type declares no rule for is reported, never passed in silence.
+using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using CsCheck;
@@ -198,7 +199,43 @@ private AutoTest(
/// edge value where a single property breaks it alone; the others (sequential rules, Idempotent, cross-property rules) need one here, and axis 09 fails without it.
///
public AutoTest CounterExamples(IReadOnlyDictionary> counterExamples) =>
- new(this, counterExamples: _counterExamples.Concat(counterExamples).GroupBy(kv => kv.Key).ToDictionary(g => g.Key, g => g.Last().Value));
+ new(
+ this,
+ counterExamples: _counterExamples
+ .Concat(counterExamples.Select(kv => new KeyValuePair>(kv.Key, AppliedOncePerBase(kv.Value))))
+ .GroupBy(kv => kv.Key)
+ .ToDictionary(g => g.Key, g => g.Last().Value)
+ );
+
+ ///
+ /// A constant generator (Gen.Const) hands every axis the same base object, and the axes run at the same time. A counter-example that changes the world its base holds
+ /// (adds a file to a MockFileSystem) would collide with itself, so it runs once per base, under a lock on that base; later applications get the value the first one made.
+ ///
+ private static Func AppliedOncePerBase(Func breakIt)
+ {
+ if (typeof(T).IsValueType)
+ {
+ return breakIt;
+ }
+ var made = new ConditionalWeakTable