1234module build56import os7import time89fn scratch(name string) !string {10 dir := os.join_path(os.temp_dir(), 'koh3-build-${name}-${time.now().unix_nano()}')11 os.mkdir_all(dir, os.MkdirParams{})!12 return dir13}1415fn write(root string, rel string, text string) ! {16 full := os.join_path(root, rel)17 os.mkdir_all(os.dir(full), os.MkdirParams{})!18 os.write_file(full, text)!19}2021struct SourceCase {22 file string23 content string24 want string25}2627fn test_every_version_source_is_read() {28 zon := '.{\n .name = "demo",\n .minimum_zig_version = "0.16.0",\n .version = "2.0.1",\n}\n'29 plist := '<?xml version="1.0"?>\n<dict>\n<key>CFBundleShortVersionString</key>\n<string>6.0.5</string>\n</dict>\n'30 cases := [31 SourceCase{'VERSION', '1.0.0\n', '1.0.0'},32 SourceCase{'build.zig.zon', zon, '2.0.1'},33 SourceCase{'v.mod', "Module {\n\tname: 'demo'\n\tversion: '3.0.2'\n}", '3.0.2'},34 SourceCase{'package.json', '{\n "name": "demo",\n "version": "4.0.3"\n}', '4.0.3'},35 SourceCase{'Cargo.toml', '[package]\nname = "demo"\nversion = "5.0.4"\n', '5.0.4'},36 SourceCase{'Info.plist', plist, '6.0.5'},37 SourceCase{'build.gradle.kts', 'versionName = "7.0.6"\n', '7.0.6'},38 SourceCase{'build.gradle', 'versionName "8.0.7"\n', '8.0.7'},39 SourceCase{'build.gradle', "versionName '9.0.8'\n", '9.0.8'},40 SourceCase{'app/build.gradle.kts', 'versionName = "10.0.9"\n', '10.0.9'},41 SourceCase{'app/build.gradle', "versionName '10.0.9'\n", '10.0.9'},42 ]43 for case in cases {44 root := scratch('source')!45 defer {46 os.rmdir_all(root) or {}47 }48 write(root, case.file, case.content)!49 assert project_version(root)! == case.want, case.file50 }51}525354fn test_a_version_file_wins_over_every_source() {55 root := scratch('override')!56 defer {57 os.rmdir_all(root) or {}58 }59 write(root, 'build.zig.zon', '.{\n .version = "2.0.1",\n}\n')!60 write(root, 'v.mod', "Module {\n\tversion: '3.0.2'\n}")!61 write(root, 'VERSION', '9.9.9\n')!62 assert project_version(root)! == '9.9.9'63}646566fn test_a_project_with_no_version_is_refused() {67 root := scratch('no_version')!68 defer {69 os.rmdir_all(root) or {}70 }71 if _ := project_version(root) {72 assert false, 'a project with no version must be refused'73 } else {74 assert err.msg().contains('VERSION')75 }76}77787980fn test_a_value_that_is_not_a_version_is_refused() {81 root := scratch('bad_version')!82 defer {83 os.rmdir_all(root) or {}84 }85 write(root, 'VERSION', '1.0.0 build 7\n')!86 if _ := project_version(root) {87 assert false, 'a value that is not a version must be refused'88 } else {89 assert err.msg().contains('VERSION')90 }91 write(root, 'VERSION', 'abcdefghijklmnopqrstuvwxyz0123456789\n')!92 if _ := project_version(root) {93 assert false, 'a version longer than 32 characters must be refused'94 } else {95 assert err.msg().contains('VERSION')96 }97}9899100101fn test_a_variable_version_name_falls_through() {102 root := scratch('gradle_variable')!103 defer {104 os.rmdir_all(root) or {}105 }106 write(root, 'app/build.gradle.kts', 'versionName = project.ext.version\n')!107 write(root, 'build.gradle', 'versionName "2.17.43"\n')!108 assert project_version(root)! == '2.17.43'109110 111 112 113 write(root, 'build.gradle', '// versionName "9.9.9"\nversionNameSuffix = "-beta"\n')!114 if _ := project_version(root) {115 assert false, 'nothing was readable, so the version must be refused'116 } else {117 assert err.msg().contains('VERSION')118 }119120 121 write(root, 'build.gradle', "versionName = providers.gradleProperty('VERSION').get()\n")122 if _ := project_version(root) {123 assert false, 'a versionName built from a function call must not be read'124 } else {125 assert err.msg().contains('VERSION')126 }127 128 129 write(root, 'Cargo.toml',130 '[package]\nversion.workspace = true\n\n[dependencies]\nserde = { version = "1.0.0" }\n')131 if _ := project_version(root) {132 assert false, 'a version delegated to the workspace must not be read'133 } else {134 assert err.msg().contains('VERSION')135 }136}137138fn test_read_config_parses_the_file_and_ignores_what_it_does_not_use() {139 root := scratch('config')!140 defer {141 os.rmdir_all(root) or {}142 }143 write(root, 'koh.build', '# how demo is built144enabled = true145trigger = on-offer146timeout_seconds = 900147command = ./make.sh148output = out/demo-{version}-{platform}149platforms = macos-aarch64, linux-aarch64150lang = Zig151')152 config := read_config(root)!153 assert config.command == './make.sh'154 assert config.output == 'out/demo-{version}-{platform}'155 assert config.platforms == ['macos-aarch64', 'linux-aarch64']156 assert config.lang == .zig157 assert output_path(config, 'linux-aarch64', '1.2.3') == 'out/demo-1.2.3-linux-aarch64'158}159160161162fn test_the_language_is_detected_from_the_project() {163 root := scratch('detect')!164 defer {165 os.rmdir_all(root) or {}166 }167 write(root, 'koh.build', 'command = true\noutput = a\nplatforms = macos-aarch64\n')!168 write(root, 'build.zig', '// a zig project\n')!169 assert read_config(root)!.lang == .zig170 write(root, 'build.sh', '#!/bin/sh\n')!171 assert read_config(root)!.lang == .shell172}173174fn test_every_defined_build_language_and_alias_is_accepted() {175 profiles := map[string]Lang{176 'v': .v177 'zig': .zig178 'rust': .rust179 'go': .go180 'c': .c181 'cpp': .cpp182 'c++': .cpp183 'node': .node184 'javascript': .node185 'typescript': .node186 'java': .java187 'kotlin': .kotlin188 'scala': .scala189 'csharp': .csharp190 'c#': .csharp191 'cs': .csharp192 'swift': .swift193 'python': .python194 'ruby': .ruby195 'php': .php196 'dart': .dart197 'elixir': .elixir198 'lua': .lua199 'r': .r200 'shell': .shell201 'make': .make202 'gradle': .gradle203 }204 for name, want in profiles {205 assert lang_of(name)? == want, name206 }207}208209fn test_mainstream_project_markers_are_detected() {210 markers := map[string]Lang{211 'v.mod': .v212 'pom.xml': .java213 'build.sbt': .scala214 'Gemfile': .ruby215 'composer.json': .php216 'pubspec.yaml': .dart217 'mix.exs': .elixir218 'DESCRIPTION': .r219 }220 for file, want in markers {221 root := scratch('detect_marker')!222 write(root, file, '')!223 assert detect(root) == want, file224 os.rmdir_all(root) or {}225 }226}227228fn test_read_config_refuses_what_it_cannot_build() {229 root := scratch('config_bad')!230 defer {231 os.rmdir_all(root) or {}232 }233 if _ := read_config(root) {234 assert false, 'a missing koh.build must be refused'235 } else {236 assert err.msg().contains('koh.build')237 }238}239240fn test_read_config_refuses_each_missing_key() {241 root := scratch('config_keys')!242 defer {243 os.rmdir_all(root) or {}244 }245 cases := [246 ['command = true\nplatforms = macos-aarch64\n', 'has no output'],247 ['output = a\nplatforms = macos-aarch64\n', 'has no command'],248 ['command = true\noutput = a\n', 'names no platforms'],249 ['command = true\noutput = a\nplatforms = macos-aarch64, amiga\n', 'amiga'],250 ['command = true\noutput = a\nplatforms = macos-aarch64, macos-aarch64\n', 'twice'],251 ['command = true\noutput = a\nplatforms = macos-aarch64\nlang = cobol\n', 'cobol'],252 ]253 for case in cases {254 write(root, 'koh.build', case[0])!255 if _ := read_config(root) {256 assert false, case[1]257 } else {258 assert err.msg().contains(case[1]), err.msg()259 }260 }261}262263fn test_the_build_environment_is_koh_2s() {264 zig := env_for(.zig, 'linux-aarch64')265 assert zig['KOH_TARGET'] == 'aarch64-linux-musl'266 assert zig['KEPR_TARGET'] == 'aarch64-linux-musl'267 268 go_env := env_for(.go, 'linux-x86_64')269 assert go_env['KOH_TARGET'] == 'linux-x86_64'270 assert go_env['KEPR_TARGET'] == 'linux-x86_64'271 assert go_env['GOOS'] == 'linux'272 assert go_env['GOARCH'] == 'amd64'273 assert env_for(.rust, 'windows-x86_64')['CARGO_BUILD_TARGET'] == 'x86_64-pc-windows-gnu'274 assert env_for(.rust, 'macos-aarch64')['CARGO_BUILD_TARGET'] == 'aarch64-apple-darwin'275 assert env_for(.c, 'linux-x86_64')['CC'] == 'x86_64-linux-musl-gcc'276 277 assert 'CC' !in env_for(.c, 'macos-x86_64')278 assert env_for(.gradle, 'android-arm64').len == 2279 assert env_for(.node, 'android-x86')['KEPR_TARGET'] == 'android-x86'280}281282fn test_this_machine_is_one_of_the_platforms() {283 assert this_machine()! in platforms284}285286287288289290fn test_build_one_runs_the_command_and_finds_the_output() {291 root := scratch('build_one')!292 defer {293 os.rmdir_all(root) or {}294 }295 write(root, 'koh.build', 'command = mkdir -p out && env > out/demo-1.2.3296output = out/demo-{version}297platforms = macos-aarch64298lang = make299')!300 config := read_config(root)!301 built := build_one(root, config, 'macos-aarch64', '1.2.3')!302 assert built.platform == 'macos-aarch64'303 assert built.path == 'out/demo-1.2.3'304 assert built.size > 0305 seen := os.read_file(os.join_path(root, 'out/demo-1.2.3'))!306 assert seen.contains('KEPR_TARGET=macos-aarch64')307 assert seen.contains('KOH_TARGET=macos-aarch64')308}309310fn test_build_one_refuses_a_failing_build_and_shows_what_it_said() {311 root := scratch('build_fail')!312 defer {313 os.rmdir_all(root) or {}314 }315 write(root, 'koh.build', 'command = sh -c \'echo "no compiler here" >&2; exit 1\'316output = out/demo317platforms = macos-aarch64318')319 config := read_config(root)!320 if _ := build_one(root, config, 'macos-aarch64', '1.2.3') {321 assert false, 'a failing build must be refused'322 } else {323 assert err.msg().contains('no compiler here')324 }325}326327328fn test_build_one_refuses_a_build_that_creates_nothing() {329 root := scratch('build_empty')!330 defer {331 os.rmdir_all(root) or {}332 }333 write(root, 'koh.build', 'command = true334output = out/demo335platforms = macos-aarch64336')337 config := read_config(root)!338 if _ := build_one(root, config, 'macos-aarch64', '1.2.3') {339 assert false, 'a build with no output must be refused'340 } else {341 assert err.msg().contains('did not create')342 }343}344345346347fn test_build_one_refuses_an_empty_artifact() {348 root := scratch('build_zero')!349 defer {350 os.rmdir_all(root) or {}351 }352 write(root, 'koh.build', 'command = mkdir -p out && : > out/demo353output = out/demo354platforms = macos-aarch64355')!356 config := read_config(root)!357 if _ := build_one(root, config, 'macos-aarch64', '1.2.3') {358 assert false, 'an empty artifact must be refused'359 } else {360 assert err.msg().contains('produced an empty')361 }362}363364365366fn test_build_one_takes_background_children_with_it() {367 root := scratch('build_orphan')!368 defer {369 os.rmdir_all(root) or {}370 }371 write(root, 'koh.build', "command = mkdir -p out && printf early > out/demo && { sh -c 'sleep 2; printf late > out/demo' & }372output = out/demo373platforms = macos-aarch64374")!375 config := read_config(root)!376 built := build_one(root, config, 'macos-aarch64', '1.2.3')!377 assert built.size == 5378 379 time.sleep(3 * time.second)380 assert os.read_file(os.join_path(root, 'out/demo'))! == 'early'381}