Files
Anubis/tests/Anubis.Tests/MiniYaml.cs
T
Alby96andClaude Opus 5.5 3cd214ae78 Publish the image on version tags and test what is delivered
A v* tag now builds, tries and publishes the image: the version must be the
one of Directory.Build.props, latest only moves forward and the registry
token is in one step, given to docker on its input. The same script works by
hand. The image says its version and commit, and its health check also
covers the engine loop and the live prices.

The Unraid template is final: a test compares it with appendix E and with
the variables the loader really reads, and another reads the workflow.

refs #70
refs #71

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 19:21:40 +01:00

178 lines
5.9 KiB
C#

using System.Globalization;
namespace Anubis.Tests;
/// <summary>
/// The part of YAML the workflow of the CI uses, for the tests only: maps, lists, plain and quoted scalars, lists of
/// strings in brackets, block text (| and >-) and comments. Anything else is refused with the line number, so a
/// workflow written in a way this reader does not understand fails the test instead of passing unread.
/// Values are <see cref="Dictionary{TKey,TValue}"/>, <see cref="List{T}"/> or <see cref="string"/>.
/// </summary>
internal sealed class MiniYaml
{
private readonly List<(int Number, int Indent, string Text)> _lines = [];
private readonly string[] _raw;
private int _at;
private MiniYaml(string text)
{
_raw = text.ReplaceLineEndings("\n").Split('\n');
for (var i = 0; i < _raw.Length; i++)
{
var indent = _raw[i].Length - _raw[i].TrimStart(' ').Length;
var body = _raw[i].Trim(' ');
if (_raw[i][indent..].StartsWith('\t'))
{
throw Bad(i + 1, "tab");
}
_lines.Add((i + 1, indent, body));
}
}
public static Dictionary<string, object> Read(string text)
{
var yaml = new MiniYaml(text);
yaml.SkipBlank();
var map = yaml.Map(0);
yaml.SkipBlank();
return yaml._at == yaml._lines.Count ? map : throw Bad(yaml._lines[yaml._at].Number, "unexpected indentation");
}
private static FormatException Bad(int line, string why) => new($"YAML line {line.ToString(CultureInfo.InvariantCulture)}: {why}.");
private void SkipBlank()
{
while (_at < _lines.Count && (_lines[_at].Text.Length == 0 || _lines[_at].Text.StartsWith('#')))
{
_at++;
}
}
private object Node(int indent, int line)
{
SkipBlank();
if (_at >= _lines.Count || _lines[_at].Indent < indent)
{
return "";
}
var found = _lines[_at];
return found.Text.StartsWith("- ", StringComparison.Ordinal) ? List(found.Indent) : found.Indent >= indent ? Map(found.Indent) : throw Bad(line, "indentation");
}
private List<object> List(int indent)
{
var list = new List<object>();
for (SkipBlank(); _at < _lines.Count && _lines[_at].Indent == indent && _lines[_at].Text.StartsWith("- ", StringComparison.Ordinal); SkipBlank())
{
var (number, _, text) = _lines[_at];
var body = text[2..].TrimStart();
if (Key(body) is null)
{
list.Add(Scalar(body, number));
_at++;
continue;
}
// "- key: value" is a map whose first line starts after the dash.
_lines[_at] = (number, indent + (text.Length - body.Length), body);
list.Add(Map(_lines[_at].Indent));
}
return list;
}
private Dictionary<string, object> Map(int indent)
{
var map = new Dictionary<string, object>(StringComparer.Ordinal);
for (SkipBlank(); _at < _lines.Count && _lines[_at].Indent == indent && !_lines[_at].Text.StartsWith("- ", StringComparison.Ordinal); SkipBlank())
{
var (number, _, text) = _lines[_at];
if (Key(text) is not { } key)
{
throw Bad(number, "not a key");
}
var rest = Uncommented(text[(key.Length + 1)..]).Trim();
_at++;
object value = rest switch
{
"" => Node(indent + 1, number),
"|" => Block(indent, "\n"),
">-" => Block(indent, " "),
_ => Scalar(rest, number),
};
if (!map.TryAdd(key, value))
{
throw Bad(number, "duplicate key " + key);
}
}
return _at < _lines.Count && _lines[_at].Indent > indent ? throw Bad(_lines[_at].Number, "unexpected indentation") : map;
}
private string Block(int indent, string join)
{
var lines = new List<string>();
var margin = -1;
while (_at < _lines.Count && (_lines[_at].Text.Length == 0 || _lines[_at].Indent > indent))
{
if (_lines[_at].Text.Length > 0)
{
margin = margin < 0 ? _lines[_at].Indent : margin;
lines.Add(_raw[_lines[_at].Number - 1][margin..]);
}
_at++;
}
return string.Join(join, lines);
}
// A key is a plain word followed by ": " or by the end of the line.
private static string? Key(string text)
{
var colon = text.IndexOf(':', StringComparison.Ordinal);
return colon > 0 && (colon == text.Length - 1 || text[colon + 1] == ' ') && text.Take(colon).All(c => char.IsAsciiLetterOrDigit(c) || c is '_' or '-') ? text[..colon] : null;
}
private static string Uncommented(string text)
{
var quote = '\0';
for (var i = 0; i < text.Length; i++)
{
if (quote != '\0')
{
quote = text[i] == quote ? '\0' : quote;
}
else if (text[i] is '"' or '\'')
{
quote = text[i];
}
else if (text[i] == '#' && i > 0 && text[i - 1] == ' ')
{
return text[..i];
}
}
return text;
}
private static object Scalar(string text, int line)
{
if (text.StartsWith('['))
{
return text.EndsWith(']') ? text[1..^1].Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries).Select(t => Scalar(t, line)).ToList()
: throw Bad(line, "open list");
}
if (text[0] is '"' or '\'' && text[^1] == text[0] && text.Length > 1)
{
return text[1..^1];
}
return text[0] is '&' or '*' or '{' or '!' or '|' or '>' or '"' or '@' or '`' ? throw Bad(line, "not supported: " + text) : text;
}
}