tazel
A build system for C, C++, Objective-C, Objective-C++ and Zig, with C# project files.
Clonegit clone https://git.progmasoft.com/tazel
Test the selector and the module and dependency checks
- Commit
29928f674fd73897034199583719e6e18259e0b9- Author
- Leitwolf11 ·
- Parents
467e8be69ad8
Changes
src/Progmasoft.Tazel/Project/Workspace.cs | 10 +-- tests/Progmasoft.Tazel.Tests/SelectorTests.cs | 87 ++++++++++++++++++++++++++ tests/Progmasoft.Tazel.Tests/WorkspaceTests.cs | 42 +++++++++++++ 3 files changed, 131 insertions(+), 8 deletions(-) diff --git a/src/Progmasoft.Tazel/Project/Workspace.cs b/src/Progmasoft.Tazel/Project/Workspace.cs index d1ab5c7..e76daef 100644 --- a/src/Progmasoft.Tazel/Project/Workspace.cs +++ b/src/Progmasoft.Tazel/Project/Workspace.cs @@ -65,8 +65,6 @@ internal sealed class ProjectTarget public bool IsManual => Settings.Tags.Contains("manual", StringComparer.Ordinal); - public bool IsLibrary => Kind is TargetKind.StaticLibrary or TargetKind.SharedLibrary; - public bool IsRunnable => Kind is TargetKind.Executable or TargetKind.Test; public bool BuildsFor(Platform platform) => Constraints.Count == 0 || Constraints.Contains(platform); @@ -422,12 +420,8 @@ internal sealed class Workspace return near is not null && Cli.Suggestions.Distance(near, label.Name) <= 2 ? $"; did you mean :{near}?" : ""; } - private static string Describe(ProjectTarget target) => target.Kind switch - { - TargetKind.Executable => "executable", - TargetKind.Test => "test", - _ => target.Kind.ToString(), - }; + /// <summary>What a program target is called in a message: only executables and tests are described.</summary> + private static string Describe(ProjectTarget target) => target.Kind == TargetKind.Test ? "test" : "executable"; private void CheckCycles() { diff --git a/tests/Progmasoft.Tazel.Tests/SelectorTests.cs b/tests/Progmasoft.Tazel.Tests/SelectorTests.cs new file mode 100644 index 0000000..c1e1f4a --- /dev/null +++ b/tests/Progmasoft.Tazel.Tests/SelectorTests.cs @@ -0,0 +1,87 @@ +// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com> +// SPDX-License-Identifier: MIT + +using System; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Progmasoft.Tazel.Cli; +using Progmasoft.Tazel.Driver; +using Progmasoft.Tazel.Project; +using Xunit; + +namespace Progmasoft.Tazel.Tests; + +/// <summary>How labels and the working directory choose targets.</summary> +public sealed class SelectorTests +{ + private const string Prelude = "using Progmasoft.Tazel.Dsl.Build.Prelude;\nusing Progmasoft.Tazel.Dsl.Common;\n"; + + private static readonly string Other = Host.Platform == Dsl.Common.Platform.LinuxAmd64 ? "MacOSArm64" : "LinuxAmd64"; + + /// <summary>A module whose root has no package: lib has two libraries, one built only for another platform, and app + /// a program that depends on that one.</summary> + private static TemporaryModule Module() => new TemporaryModule() + .Write("MODULE.csx", TemporaryModule.ModuleFile) + .Write("lib/BUILD.csx", Prelude + $$""" + CC.StaticLibrary("util", new() { Sources = ["u.c"], Visibility = Visibility.Public }); + CC.StaticLibrary("elsewhere", new() { Sources = ["u.c"], Visibility = Visibility.Public, TargetConstraints = [Platform.{{Other}}] }); + """) + .Write("lib/u.c", "") + .Write("lib/sub/notes.txt", "") + .Write("app/BUILD.csx", Prelude + """ + CC.Executable("app", new() { Sources = ["main.c"], Depends = ["//lib:elsewhere"] }); + """) + .Write("app/main.c", ""); + + private static string[] Names(System.Collections.Generic.IEnumerable<ProjectTarget> targets) => + [.. targets.Select(target => target.Label.ToString())]; + + [Fact] + public async Task WithoutLabelsTheNearestPackageAboveTheDirectoryIsChosen() + { + using var module = Module(); + var selector = new Selector(await module.LoadAsync(), Host.Platform); + var chosen = selector.Select([], Path.Combine(module.Root, "lib", "sub"), _ => true, "a target"); + Assert.Equal(["//lib:util"], Names(chosen)); + Assert.Equal(1, selector.Skipped); + } + + [Fact] + public async Task WithoutLabelsADirectoryInNoPackageIsAnError() + { + using var module = Module(); + var selector = new Selector(await module.LoadAsync(), Host.Platform); + var error = Assert.Throws<ProjectException>(() => selector.Select([], module.Root, _ => true, "a target")); + Assert.Contains("the current directory is in no package", error.Message, StringComparison.Ordinal); + } + + [Theory] + [InlineData("//lib:a:b", "")] + [InlineData("//nothing/...", "//nothing/... names no package")] + [InlineData("//lib:missing", "its package has no target 'missing'")] + [InlineData("//elsewhere:x", "there is no package //elsewhere")] + [InlineData("//app:app", "it depends on //lib:elsewhere, which does not")] + public async Task ANamedTargetThatCannotBeBuiltIsAnError(string label, string message) + { + using var module = Module(); + var selector = new Selector(await module.LoadAsync(), Host.Platform); + var error = Assert.Throws<ProjectException>(() => selector.Select([label], module.Root, _ => true, "a target")); + Assert.Contains(message, error.Message, StringComparison.Ordinal); + } + + [Fact] + public async Task APatternLeavesOutAndCountsWhatDoesNotBuildAndANamedTargetIsChosenOnce() + { + using var module = Module(); + var selector = new Selector(await module.LoadAsync(), Host.Platform); + var chosen = selector.Select(["//...", "//lib:util"], module.Root, _ => true, "a target"); + Assert.Equal(["//lib:util"], Names(chosen)); + Assert.Equal(2, selector.Skipped); + // A dependency's reason is kept: asking again gives the same answer. + var workspace = await module.LoadAsync(); + var again = new Selector(workspace, Host.Platform); + var app = workspace.Targets.Values.Single(target => target.Label.Name == "app"); + Assert.Equal(again.Incompatibility(app), again.Incompatibility(app)); + } +} diff --git a/tests/Progmasoft.Tazel.Tests/WorkspaceTests.cs b/tests/Progmasoft.Tazel.Tests/WorkspaceTests.cs index 951d088..16328bf 100644 --- a/tests/Progmasoft.Tazel.Tests/WorkspaceTests.cs +++ b/tests/Progmasoft.Tazel.Tests/WorkspaceTests.cs @@ -124,4 +124,46 @@ public sealed class WorkspaceTests var error = await Assert.ThrowsAsync<ProjectException>(() => module.LoadAsync()); Assert.Contains("no toolchain //toolchains:clang is declared", error.Message, StringComparison.Ordinal); } + + private static string Module(string group = "com.example", string name = "sample", string extra = "") => $$""" + using Progmasoft.Tazel.Dsl.Module.Prelude; + module = Module.Define(new() { Group = "{{group}}", Name = "{{name}}", Version = "0.1.0" }); + {{extra}} + """; + + [Theory] + [InlineData("com.example", "bad name", "", "the module name 'bad name' must be letters")] + [InlineData("com..example", "sample", "", "the group 'com..example' must be names joined by dots")] + [InlineData("", "sample", "", "the group '' must be names joined by dots")] + [InlineData("com.example", "sample", "dependencies = Dependencies.Define(LocalModule.From(new() { Name = \"no way\", Include = \"x.tazel.tar.xz\", Sha256 = \"00\" }));", "the local module name 'no way' must be letters")] + [InlineData("com.example", "sample", "toolchains = Toolchains.Define(\"//:a:b\");", "MODULE.csx: ")] + public async Task AModuleFileWithAWrongNameIsRefused(string group, string name, string extra, string message) + { + using var module = new TemporaryModule(); + module.Write("MODULE.csx", Module(group, name, extra)).Write("BUILD.csx", Prelude); + var error = await Assert.ThrowsAsync<ProjectException>(() => module.LoadAsync()); + Assert.Contains(message, error.Message, StringComparison.Ordinal); + } + + [Theory] + [InlineData("CC.Executable(\"a\", new() { Depends = [\":t\"] }); CC.Test(\"t\", new());", "//:t is a test; only libraries are linked")] + [InlineData("Zig.Module(\"m\", new() { RootSourceFile = \"m.zig\" }); Zig.Executable(\"a\", new() { RootSourceFile = \"a.zig\", Depends = [\":m\"] });", "//:m is a Zig module; name it in ModuleImports")] + [InlineData("CC.StaticLibrary(\"l\", new()); Zig.Executable(\"a\", new() { RootSourceFile = \"a.zig\", ModuleImports = new System.Collections.Generic.Dictionary<string, string> { [\"l\"] = \":l\" } });", "ModuleImports[\"l\"] names //:l, which is not a Zig module")] + [InlineData("CC.Executable(\"a\", new() { Depends = [\"//:x:y\"] });", "BUILD.csx:2: ")] + public async Task ADependencyOfTheWrongKindIsRefused(string body, string message) + { + using var module = new TemporaryModule(); + module.Write("MODULE.csx", TemporaryModule.ModuleFile).Write("BUILD.csx", Prelude + body); + var error = await Assert.ThrowsAsync<ProjectException>(() => module.LoadAsync()); + Assert.Contains(message, error.Message, StringComparison.Ordinal); + } + + [Fact] + public async Task ATargetIsWrittenAsItsLabel() + { + using var module = new TemporaryModule(); + module.Write("MODULE.csx", TemporaryModule.ModuleFile).Write("BUILD.csx", Prelude + "CC.StaticLibrary(\"core\", new());"); + var workspace = await module.LoadAsync(); + Assert.Equal("//:core", workspace.Targets.Values.Single().ToString()); + } }