Progmasoft Git

tazel

A build system for C, C++, Objective-C, Objective-C++ and Zig, with C# project files.

Clonegit clone https://git.progmasoft.com/tazel

mainroot / tests / Progmasoft.Tazel.Tests / WorkspaceTests.cs

6.2 KiB · Raw · Log

// SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com>
// SPDX-License-Identifier: MIT

using System;
using System.Linq;
using System.Threading.Tasks;
using Progmasoft.Tazel.Dsl;
using Progmasoft.Tazel.Dsl.Build.Prelude;
using Progmasoft.Tazel.Label;
using Progmasoft.Tazel.Project;
using Xunit;

namespace Progmasoft.Tazel.Tests;

public sealed class WorkspaceTests
{
    private const string Prelude = "using Progmasoft.Tazel.Dsl.Build.Prelude;\n";

    [Fact]
    public async Task LoadsTheTargetsOfEveryPackage()
    {
        using var module = new TemporaryModule();
        module.Write("MODULE.csx", TemporaryModule.ModuleFile)
            .Write("BUILD.csx", Prelude + """
                var core = CC.StaticLibrary("core", new() { Sources = ["core.c"], Visibility = Visibility.Module });
                CC.Executable("app", new() { Sources = ["main.c"], Depends = ["//:core", "//lib:util"] });
                """)
            .Write("core.c", "").Write("main.c", "")
            .Write("lib/BUILD.csx", Prelude + """
                CC.StaticLibrary("util", new() { Sources = new Glob("*.c"), Visibility = Visibility.Public });
                """)
            .Write("lib/u.c", "");
        var workspace = await module.LoadAsync();
        var app = workspace.Targets[new TargetLabel(PackageId.Root, "app")];
        Assert.Equal(TargetKind.Executable, app.Kind);
        Assert.Equal(["//:core", "//lib:util"], app.Dependencies.Select(d => d.Label.ToString()));
        Assert.Equal("BUILD.csx:3", app.Place);
        Assert.Equal(["//", "//lib"], workspace.Packages.Select(p => p.ToString()).Order(StringComparer.Ordinal));
    }

    [Theory]
    [InlineData("CC.Executable(\"a\", new() { Depends = [\":nope\"] });", "depends on //:nope, which is not declared")]
    [InlineData("CC.Executable(\"a\", new() { Depends = [\":b\"] }); CC.Executable(\"b\", new());", "only libraries are linked")]
    [InlineData("CC.StaticLibrary(\"a\", new()); CC.StaticLibrary(\"a\", new());", "'a' is declared twice")]
    [InlineData("CC.StaticLibrary(\"a\", new() { Depends = [\":b\"] }); CC.StaticLibrary(\"b\", new() { Depends = [\":a\"] });", "depend on each other")]
    [InlineData("CC.StaticLibrary(\"bad name\", new());", "must be letters")]
    [InlineData("CC.StaticLibrary(\"a\", new() { Sources = [\"missing.c\"] });", "")]
    [InlineData("CC.StaticLibrary(\"a\", new() { Nope = 1 });", "BUILD.csx:2:")]
    [InlineData("throw new System.InvalidOperationException(\"no\");", "InvalidOperationException: no")]
    public async Task ReportsWhatIsWrongWithItsPlace(string body, string message)
    {
        using var module = new TemporaryModule();
        module.Write("MODULE.csx", TemporaryModule.ModuleFile).Write("BUILD.csx", Prelude + body);
        if (message.Length == 0)
        {
            // A missing source is found when the target is planned, not when the file runs.
            var workspace = await module.LoadAsync();
            Assert.Single(workspace.Targets);
            return;
        }

        var error = await Assert.ThrowsAsync<ProjectException>(() => module.LoadAsync());
        Assert.Contains(message, error.Message, StringComparison.Ordinal);
    }

    [Fact]
    public async Task APrivateTargetIsSeenOnlyInItsFile()
    {
        using var module = new TemporaryModule();
        module.Write("MODULE.csx", TemporaryModule.ModuleFile)
            .Write("BUILD.csx", Prelude + "CC.Executable(\"app\", new() { Depends = [\"//lib:hidden\"] });")
            .Write("lib/BUILD.csx", Prelude + "CC.StaticLibrary(\"hidden\", new());");
        var error = await Assert.ThrowsAsync<ProjectException>(() => module.LoadAsync());
        Assert.Contains("//lib:hidden is private", error.Message, StringComparison.Ordinal);
    }

    [Fact]
    public async Task AModuleFileMustDefineAValidModule()
    {
        using var module = new TemporaryModule();
        module.Write("MODULE.csx", """
            using Progmasoft.Tazel.Dsl.Module.Prelude;
            module = Module.Define(new() { Group = "com.example", Name = "x", Version = "1.2" });
            """);
        var error = await Assert.ThrowsAsync<ProjectException>(() => module.LoadAsync());
        Assert.Contains("'1.2' is not a SemVer version", error.Message, StringComparison.Ordinal);
    }

    [Fact]
    public async Task AModuleSaysWhichTazelItBuildsWith()
    {
        using var module = new TemporaryModule();
        module.Write("MODULE.csx", """
            using Progmasoft.Tazel.Dsl.Module.Prelude;
            module = Module.Define(new() { Group = "com.example", Name = "x", Version = "1.0.0",
                Compatibility = TazelVersion.AtLeast(9, 0) });
            """);
        var error = await Assert.ThrowsAsync<ProjectException>(() => module.LoadAsync());
        Assert.Contains("builds with Tazel 9.0.0 or later; this is Tazel 0.1.0", error.Message, StringComparison.Ordinal);
    }

    [Fact]
    public async Task ObjectiveCTargetsBuildForMacOSUnlessTheySayOtherwise()
    {
        using var module = new TemporaryModule();
        module.Write("MODULE.csx", TemporaryModule.ModuleFile).Write("BUILD.csx", Prelude + """
            ObjectiveC.StaticLibrary("mac", new());
            ObjectiveC.StaticLibrary("gnustep", new() { TargetConstraints = [Platform.LinuxAmd64] });
            """);
        var workspace = await module.LoadAsync();
        var mac = workspace.Targets[new TargetLabel(PackageId.Root, "mac")];
        var gnustep = workspace.Targets[new TargetLabel(PackageId.Root, "gnustep")];
        Assert.True(mac.BuildsFor(Dsl.Common.Platform.MacOSArm64));
        Assert.False(mac.BuildsFor(Dsl.Common.Platform.LinuxAmd64));
        Assert.True(gnustep.BuildsFor(Dsl.Common.Platform.LinuxAmd64));
    }

    [Fact]
    public async Task ARegisteredToolchainMustBeDeclared()
    {
        using var module = new TemporaryModule();
        module.Write("MODULE.csx", TemporaryModule.ModuleFile + "\ntoolchains = Toolchains.Define(\"//toolchains:clang\");")
            .Write("toolchains/BUILD.csx", "using Progmasoft.Tazel.Dsl.Build.Toolchain.Prelude;\n");
        var error = await Assert.ThrowsAsync<ProjectException>(() => module.LoadAsync());
        Assert.Contains("no toolchain //toolchains:clang is declared", error.Message, StringComparison.Ordinal);
    }
}