Merge pull request 'IpaGestureExperiment onto StoreFileName (stacked on #11)' (#14) from aqua/store-filename-ipa into main

This commit is contained in:
Aqua 2026-11-07 10:39:13 +00:00
commit 84f892fc99
4 changed files with 67 additions and 11 deletions

View file

@ -108,7 +108,7 @@ public static class IpaGestureExperiment
throw new ArgumentException("At least one experiment variant is required.", nameof(variants));
}
var roundDirectory = Path.Combine(rootDirectory, SafeFileName(roundId));
var roundDirectory = Path.Combine(rootDirectory, StoreFileName.From(roundId));
var candidateDirectory = Path.Combine(roundDirectory, "candidates");
var timelineDirectory = Path.Combine(roundDirectory, "timelines");
Directory.CreateDirectory(candidateDirectory);
@ -122,7 +122,7 @@ public static class IpaGestureExperiment
{
foreach (var variant in variantList)
{
var candidateId = SafeFileName($"{target.Id}-{variant.Id}");
var candidateId = StoreFileName.From($"{StoreFileName.From(target.Id)}/{StoreFileName.From(variant.Id)}");
var script = BuildCandidateScript(target, variant);
var scriptPath = Path.Combine(candidateDirectory, $"{candidateId}.aqua");
File.WriteAllText(scriptPath, script, Encoding.UTF8);
@ -705,13 +705,6 @@ public static class IpaGestureExperiment
private static string SafeDslName(string value) =>
new(value.Select(character => char.IsLetterOrDigit(character) ? character : '_').ToArray());
private static string SafeFileName(string value)
{
var invalid = Path.GetInvalidFileNameChars().ToHashSet();
var safe = new string(value.Select(character => invalid.Contains(character) || char.IsWhiteSpace(character) ? '-' : character).ToArray());
return safe.Length == 0 ? "candidate" : safe;
}
private static string Format(float value) =>
value.ToString("0.######", CultureInfo.InvariantCulture);

View file

@ -13,6 +13,11 @@ namespace AquaSynth.Dsl;
/// rewritten names always do, so the two sets cannot meet; distinct ids stay distinct up to a 64-bit
/// hash. The result is never a path: no separators, never "." or "..".
/// </summary>
/// <remarks>
/// Not handled: symlinks and junctions already inside a store are followed, and Windows 8.3 short names
/// were not verified. An id with an uppercase letter is rewritten, so a batch directory named for such
/// an id gains a hash suffix.
/// </remarks>
public static class StoreFileName
{
private const int MaxStem = 100;

View file

@ -118,6 +118,36 @@ public sealed class StoreFileNameTests
}
}
[Fact]
public void IpaRoundCandidatesWithCollidingSanitisedIdsKeepDistinctFilesAndStayInRoot()
{
var root = Path.Combine(Path.GetTempPath(), $"aquasynth-ipa-sfn-{Guid.NewGuid():N}");
var result = IpaGestureExperiment.WriteRound(
root,
"round-sfn",
[new IpaGestureExperimentTarget("a/b", "p", "voiceless_bilabial_plosive", .1f), new IpaGestureExperimentTarget("a-b", "m", "voiced_bilabial_nasal", .1f)],
[new IpaGestureExperimentVariant("base")]);
Assert.Equal(2, result.Candidates.Select(c => c.Id).Distinct(StringComparer.OrdinalIgnoreCase).Count());
Assert.All(result.Candidates, c => Assert.StartsWith(Path.GetFullPath(root), Path.GetFullPath(c.ScriptPath)));
Assert.Throws<ArgumentException>(() => IpaGestureExperiment.WriteRound(root, "..", [new IpaGestureExperimentTarget("a", "p", "voiceless_bilabial_plosive")], [new IpaGestureExperimentVariant("base")]));
}
[Fact]
public void IpaRoundJoinedTargetVariantIdsThatConcatenateEquallyKeepDistinctFiles()
{
var root = Path.Combine(Path.GetTempPath(), $"aquasynth-ipa-join-{Guid.NewGuid():N}");
var result = IpaGestureExperiment.WriteRound(
root,
"round-join",
[new IpaGestureExperimentTarget("a", "p", "voiceless_bilabial_plosive", .1f), new IpaGestureExperimentTarget("a-b", "m", "voiced_bilabial_nasal", .1f)],
[new IpaGestureExperimentVariant("b-c"), new IpaGestureExperimentVariant("c")]);
Assert.Equal(4, result.Candidates.Count);
Assert.Equal(4, result.Candidates.Select(c => c.Id).Distinct(StringComparer.OrdinalIgnoreCase).Count());
Assert.Equal(4, result.Candidates.Select(c => c.ScriptPath).Distinct(StringComparer.OrdinalIgnoreCase).Count());
}
[Theory]
[InlineData("../x")]
[InlineData("..\\x")]
@ -130,6 +160,33 @@ public sealed class StoreFileNameTests
Assert.Equal(Path.GetFullPath(root), Path.GetDirectoryName(full));
}
[Theory]
[InlineData("\\\\host\\share\\x")]
[InlineData("C:x")]
[InlineData("a:b")]
[InlineData("x.")]
[InlineData("x ")]
[InlineData("x. ")]
public void WindowsAbsoluteAndTrailingDotOrSpaceIdsStayContainedAndPlain(string id)
{
var root = Path.Combine(Path.GetTempPath(), "artifact-root-probe", "artifacts");
var name = StoreFileName.From(id);
Assert.Matches("^[A-Za-z0-9._-]+$", name);
Assert.False(name.EndsWith('.') || name.EndsWith(' '));
Assert.Equal(Path.GetFullPath(root), Path.GetDirectoryName(Path.GetFullPath(StoreFileName.Combine(root, id))));
}
[Theory]
[InlineData("../x")]
[InlineData("a/b")]
[InlineData("name:with:colons")]
public void LoweredDspFileNameStaysInTheOutputDirectory(string name)
{
var outDir = Path.Combine(Path.GetTempPath(), "aqualower-probe", "out");
var file = StoreFileName.From(name) + ".dsp";
Assert.Equal(Path.GetFullPath(outDir), Path.GetDirectoryName(Path.GetFullPath(Path.Combine(outDir, file))));
}
[Fact]
public void BareDotDotBatchIdIsRefused() =>
Assert.Throws<ArgumentException>(() => StoreFileName.Combine(Path.GetTempPath(), ".."));

View file

@ -47,8 +47,9 @@ foreach (var spec in args.Skip(2))
warnings.AddRange(export.Warnings);
}
else source = text;
File.WriteAllText(Path.Combine(outDir, name + ".dsp"), source);
entries.Add(new { name, origin = rel.Replace('\\', '/'), kind = path.EndsWith(".aqua", StringComparison.Ordinal) ? "aqua" : "faust", dsp = name + ".dsp", warnings });
var dspFile = StoreFileName.From(name) + ".dsp";
File.WriteAllText(Path.Combine(outDir, dspFile), source);
entries.Add(new { name, origin = rel.Replace('\\', '/'), kind = path.EndsWith(".aqua", StringComparison.Ordinal) ? "aqua" : "faust", dsp = dspFile, warnings });
Console.WriteLine($"{name}: {source.Length} chars from {rel}, {warnings.Count} warnings");
}
File.WriteAllText(Path.Combine(outDir, "catalog.json"), JsonSerializer.Serialize(entries, new JsonSerializerOptions { WriteIndented = true }));