release_test.v

8.1 KB · raw

1//! Folder-backend tests for publishing: what lands in the bucket, what a retry2//! does, what a second build of the same version does, and the notes rule.34module release56import json27import os8import src.build9import src.history10import src.machine11import src.remote12import src.snapshot13import src.storage1415// Global rules must not make source fixtures depend on the developer’s home.16fn testsuite_begin() {17	os.setenv('HOME', os.join_path(os.temp_dir(), 'koh3-test-home-${os.getpid()}'), true)18}1920fn scratch(name string) !string {21	dir := os.join_path(os.temp_dir(), 'koh3_release_${os.getpid()}_${name}')22	os.rmdir_all(dir) or {}23	os.mkdir_all(dir, os.MkdirParams{})!24	return dir25}2627fn write(root string, rel string, text string) ! {28	full := os.join_path(root, rel)29	os.mkdir_all(os.dir(full), os.MkdirParams{})!30	os.write_file(full, text)!31}3233fn open_repo(name string) !storage.Store {34	dir := os.join_path(os.temp_dir(), 'koh3_release_proj_${os.getpid()}_${name}')35	os.rmdir_all(dir) or {}36	os.mkdir_all(dir, os.MkdirParams{})!37	mut store := storage.create(dir)!38	write(dir, '.kohignore', '*.log\n')!39	return store40}4142fn make_save(store &storage.Store, message string) !history.Save {43	return history.save_worktree(store, store.root, snapshot.load(store.root), history.SaveInput{44		message: message45	})!46}4748// The build writes the file `output` names, by hand: only `output` takes the49// {version} and {platform} tokens.50fn demo_config(store &storage.Store, platforms string) !build.Config {51	write(store.root, 'VERSION', '1.2.3\n')!52	mut command := 'mkdir -p out'53	for platform in platforms.split(',') {54		trimmed := platform.trim_space()55		command += ' && printf koh > out/demo-1.2.3-${trimmed}'56	}57	write(store.root, 'koh.build', 'command = ${command}58output = out/demo-{version}-{platform}59platforms = ${platforms}60')!61	return build.read_config(store.root)!62}6364fn test_publishing_writes_the_files_the_record_and_latest() {65	base := scratch('publish')!66	defer {67		os.rmdir_all(base) or {}68	}69	mut store := open_repo('publish')!70	defer {71		store.close()72	}73	config := demo_config(store, 'macos-aarch64')!74	head := make_save(store, 'first')!75	mut backend := remote.new_folder(os.join_path(base, 'remote'))!7677	published := publish(store, mut backend, 'proj', config, '1.2.3', '')!78	assert published.version == '1.2.3'79	assert published.files.len == 18081	name := 'demo-1.2.3-macos-aarch64'82	artifact := backend.get(file_key('proj', '1.2.3', 'macos-aarch64', name))!83	assert artifact.found84	assert artifact.body.bytestr() == 'koh'8586	record := json2.decode[Record](backend.get(release_key('proj', '1.2.3'))!.body.bytestr())!87	assert record.format == 188	assert record.repo == 'proj'89	assert record.version == '1.2.3'90	assert record.save == head.id91	assert record.machine == machine.name()92	assert record.notes == ''93	assert record.files.len == 194	assert record.files[0].platform == 'macos-aarch64'95	assert record.files[0].name == name96	assert record.files[0].size == 397	assert record.files[0].blake3 == storage.hash_hex('koh'.bytes())98	assert record.files[0].sha256.len == 6499	// The record is what makes the version exist, and latest moves last.100	assert backend.get(latest_key('proj'))!.body.bytestr() == '1.2.3'101}102103// An interrupted publish leaves files behind; running it again finishes it,104// because the same bytes are the same release.105fn test_a_publish_retries_after_a_partial_upload() {106	base := scratch('retry')!107	defer {108		os.rmdir_all(base) or {}109	}110	mut store := open_repo('retry')!111	defer {112		store.close()113	}114	config := demo_config(store, 'macos-aarch64, linux-aarch64')!115	make_save(store, 'first')!116	mut backend := remote.new_folder(os.join_path(base, 'remote'))!117118	publish(store, mut backend, 'proj', config, '1.2.3', '')!119	// Stand in for a run that stopped before the record and the pointer: both120	// are removed, the uploaded files stay.121	backend.delete(release_key('proj', '1.2.3'))!122	backend.delete(latest_key('proj'))!123	again := publish(store, mut backend, 'proj', config, '1.2.3', '')!124	assert again.files.len == 2125	assert backend.get(release_key('proj', '1.2.3'))!.found126	assert backend.get(latest_key('proj'))!.body.bytestr() == '1.2.3'127	assert backend.get(file_key('proj', '1.2.3', 'macos-aarch64', 'demo-1.2.3-macos-aarch64'))!.found128	assert backend.get(file_key('proj', '1.2.3', 'linux-aarch64', 'demo-1.2.3-linux-aarch64'))!.found129}130131// Two platforms that build to one file are refused before anything runs: no132// build, no upload, and the bucket is untouched.133fn test_two_platforms_building_to_one_file_are_refused() {134	base := scratch('collision')!135	defer {136		os.rmdir_all(base) or {}137	}138	mut store := open_repo('collision')!139	defer {140		store.close()141	}142	write(store.root, 'VERSION', '1.2.3\n')!143	// No {platform}, so both platforms would write the same file.144	write(store.root, 'koh.build', 'command = mkdir -p out && printf koh > out/demo145output = out/demo146platforms = macos-aarch64, linux-aarch64147')!148	config := build.read_config(store.root)!149	make_save(store, 'first')!150	mut backend := remote.new_folder(os.join_path(base, 'remote'))!151152	if _ := publish(store, mut backend, 'proj', config, '1.2.3', '') {153		assert false, 'two platforms building to one file must be refused'154	} else {155		assert err.msg().contains('two platforms build to the same file')156	}157	assert !os.exists(os.join_path(store.root, 'out'))158	assert backend.list('proj/releases')!.len == 0159}160161// A published version never changes: a second build with different bytes for162// the same platform is refused before anything is uploaded.163fn test_a_different_file_for_a_published_version_is_refused() {164	base := scratch('changed')!165	defer {166		os.rmdir_all(base) or {}167	}168	mut store := open_repo('changed')!169	defer {170		store.close()171	}172	config := demo_config(store, 'macos-aarch64')!173	make_save(store, 'first')!174	mut backend := remote.new_folder(os.join_path(base, 'remote'))!175	name := 'demo-1.2.3-macos-aarch64'176	backend.put_if_absent(file_key('proj', '1.2.3', 'macos-aarch64', name), 'other'.bytes())!177178	if _ := publish(store, mut backend, 'proj', config, '1.2.3', '') {179		assert false, 'a different file for a published version must be refused'180	} else {181		assert err.msg().contains('already has a different ${name}')182	}183	// Nothing else was written: no record, and latest never moved.184	assert !backend.get(release_key('proj', '1.2.3'))!.found185	assert !backend.get(latest_key('proj'))!.found186}187188// One platform failing stops the release before the first upload.189fn test_a_failed_build_uploads_nothing() {190	base := scratch('failed')!191	defer {192		os.rmdir_all(base) or {}193	}194	mut store := open_repo('failed')!195	defer {196		store.close()197	}198	// The command writes only the macos file, so the linux build produces199	// nothing and the whole release stops there.200	write(store.root, 'VERSION', '1.2.3\n')!201	write(store.root, 'koh.build', 'command = mkdir -p out && printf koh > out/demo-1.2.3-macos-aarch64202output = out/demo-{version}-{platform}203platforms = macos-aarch64, linux-aarch64204')!205	config := build.read_config(store.root)!206	make_save(store, 'first')!207	mut backend := remote.new_folder(os.join_path(base, 'remote'))!208209	if _ := publish(store, mut backend, 'proj', config, '1.2.3', '') {210		assert false, 'a build that produced nothing must stop the release'211	} else {212		assert err.msg().contains('did not create')213	}214	assert backend.list('proj/releases')!.len == 0215}216217// A folder where a file belongs, or a name Kepr would refuse to read, is218// refused here: Kepr refuses the whole repo's sync over one bad record.219fn test_a_release_file_name_kepr_would_refuse_is_refused() {220	assert good_name('demo-1.2.3-android-arm64.apk')221	assert good_name('Haven.app.zip')222	assert !good_name('')223	assert !good_name('.')224	assert !good_name('..')225	assert !good_name('a/b')226	assert !good_name('a\\b')227	assert !good_name('a"b')228	assert !good_name('a\x07b')229	assert !good_name('x'.repeat(256))230}231232fn test_the_notes_rule_is_koh_2s() {233	check_notes('')!234	check_notes('   ')!235	check_notes('fixed the crash on startup when the config file was missing entirely')!236	if _ := check_notes('too short to say anything at all') {237		assert false, 'notes under 40 characters must be refused'238	}239	if _ := check_notes('this note is long enough in characters but few words') {240		assert false, 'notes under 12 words must be refused'241	}242}