From f0711a7e22d94c87706a83c255e41f1821bdac92 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Fri, 14 Feb 2025 02:58:15 -0800 Subject: [PATCH] update to 0.13 and add tests --- build.zig | 12 ++++++++++++ licenses.txt | 1 + src/lib.zig | 2 -- test.zig | 19 +++++++++++++++++++ zig.mod | 2 ++ 5 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 test.zig diff --git a/build.zig b/build.zig index 7457a7ba56b4a8cd377f0ab5edffe1d64e04243d..357a03f719f6499ebc393c7e1b1118c3003ee7f5 100644 --- a/build.zig +++ b/build.zig @@ -22,4 +22,16 @@ pub fn build(b: *std.Build) void { const run_step = b.step("run", "Run the app"); run_step.dependOn(&run_cmd.step); + + const tests = b.addTest(.{ + .root_source_file = b.path("test.zig"), + .target = target, + .optimize = mode, + }); + deps.addAllTo(tests); + + const test_step = b.step("test", "Run all library tests"); + const tests_run = b.addRunArtifact(tests); + tests_run.has_side_effects = true; + test_step.dependOn(&tests_run.step); } diff --git a/licenses.txt b/licenses.txt index d8f226d062631836e7753ec6743914996176a6cd..596e4b579bcf9aeb2f13123dae1d7f674e783bfb 100644 --- a/licenses.txt +++ b/licenses.txt @@ -1,5 +1,6 @@ MIT: = https://spdx.org/licenses/MIT - This +- git https://github.com/nektro/zig-expect - git https://github.com/nektro/zig-leven - git https://github.com/nektro/zig-licenses-text diff --git a/src/lib.zig b/src/lib.zig index 37da6c1bbce0fd16af7fade45d8894c396347316..74a7a29f7169ac6b435848701a2b74e6182b2810 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -14,7 +14,6 @@ pub fn detect(alloc: std.mem.Allocator, license_src: []const u8) ![]const u8 { ind = i; } } - return licenses.spdx[ind.?][0]; } @@ -23,7 +22,6 @@ pub fn detectInDir(alloc: std.mem.Allocator, dir: std.fs.Dir) !?[]const u8 { (try testLicenseFile(alloc, dir, "LICENSE.md")) orelse (try testLicenseFile(alloc, dir, "LICENSE.txt")) orelse return null; - defer alloc.free(b); return try detect(alloc, b); } diff --git a/test.zig b/test.zig new file mode 100644 index 0000000000000000000000000000000000000000..3a396aeb822fa11d104ba6e0ed2d33a3da5e77dc --- /dev/null +++ b/test.zig @@ -0,0 +1,19 @@ +const std = @import("std"); +const detect = @import("detect-license"); +const expect = @import("expect").expect; + +test { + std.testing.refAllDeclsRecursive(detect); +} +test { + const alloc = std.testing.allocator; + const src = @embedFile("./LICENSE"); + const license = try detect.detect(alloc, src); + try expect(license).toEqualString("MIT"); +} +test { + const alloc = std.testing.allocator; + const dir = std.fs.cwd(); + const license = try detect.detectInDir(alloc, dir); + try expect(license).toEqualString("MIT"); +} diff --git a/zig.mod b/zig.mod index 46ba11e6430aae87cd4c383d187c78ed424459c8..0bc65020fe091addb9f3e945dfc8b8233e0a18c7 100644 --- a/zig.mod +++ b/zig.mod @@ -6,3 +6,5 @@ description: Given an input text guess which SPDX license it most likely is an i dependencies: - src: git https://github.com/nektro/zig-licenses-text - src: git https://github.com/nektro/zig-leven +root_dependencies: + - src: git https://github.com/nektro/zig-expect -- 2.54.0