snapshot_test.v

11.8 KB · raw

1module snapshot23import os4import time5import src.storage67fn temp_root(name string) !string {8	dir := os.join_path(os.temp_dir(), 'koh3_snap_${os.getpid()}_${name}')9	os.rmdir_all(dir) or {}10	os.mkdir_all(dir, os.MkdirParams{})!11	return dir12}1314fn put(root string, rel string, text string) ! {15	full := os.join_path(root, rel)16	os.mkdir_all(os.dir(full), os.MkdirParams{})!17	os.write_file(full, text)!18}1920fn test_ignore_matches_the_documented_shapes() {21	ig := parse('22# a comment23*.log24build/25/anchored.txt26out/**27docs/*.md28')29	assert ig.matches('error.log', false)30	assert ig.matches('deep/nested/error.log', false)31	assert !ig.matches('error.txt', false)32	// A trailing slash names a directory: the directory itself and everything33	// below it, but not a file that shares the name.34	assert ig.matches('build', true)35	assert ig.matches('build/thing.o', false)36	assert ig.matches('src/build/nested.o', false)37	assert !ig.matches('build', false)38	assert ig.matches('anchored.txt', false)39	assert !ig.matches('sub/anchored.txt', false)40	assert ig.matches('out/anything/at/all', false)41	assert ig.matches('docs/readme.md', false)42	assert !ig.matches('docs/readme.txt', false)43	assert !ig.matches('src/main.v', false)44}4546// A stripped path is banned as itself: its `*` and `?` must not become47// wildcards, in `.kohignore` or in the walk's ban.48fn test_a_stripped_path_with_wildcard_characters_bans_only_itself() {49	root := temp_root('literal_ban')!50	defer {51		os.rmdir_all(root) or {}52	}53	assert add_ignore(root, 'keys/a*b?.env')!54	assert os.read_file(os.join_path(root, ignore_file))! == '/keys/a\\*b\\?.env\n'55	for ig in [load(root), Ignore{}.banning(['keys/a*b?.env'])] {56		assert ig.matches('keys/a*b?.env', false)57		assert !ig.matches('keys/axxbz.env', false)58		assert !ig.matches('keys/a*b?.env.bak', false)59	}60	assert parse('\\\\x').matches('\\x', false)61}6263fn test_hard_ignores_are_not_overridable() {64	for path in ['.koh', '.koh-wal', '.koh-shm', '.koh-v2', '.koh-v2/snapshots/x.json', '.git',65		'.git/config', 'LOG.md'] {66		assert hard_ignored(path), '${path} should be hard ignored'67	}68	assert !hard_ignored('.kohignore')69	assert !hard_ignored('src/log.md')70	// A repo inside the repo keeps its own history: restoring an old copy of71	// its .koh or .git would roll that history back.72	for path in ['inner/.koh', 'inner/.koh-wal', 'inner/.koh-shm', 'a/b/.git', 'vendored/.git/HEAD',73		'old/.koh-v2/snapshots/x.json', 'koh2repo/.koh/objects/ab/cd'] {74		assert hard_ignored(path), '${path} should be hard ignored'75	}76	assert !hard_ignored('inner/.kohignore')77	assert !hard_ignored('docs/.github/workflow.yml')78	assert !hard_ignored('notes/.koh.md')79	// A pattern list cannot un-ignore them, and `.kohignore` itself is content.80	ig := parse('')81	assert ig.matches('.koh', false)82	assert !ig.matches('.kohignore', false)83}8485fn test_walk_skips_ignored_files_directories_and_symlinks() {86	mut store := storage.create(temp_root('walk')!)!87	root := store.root88	defer {89		store.close()90		os.rmdir_all(root) or {}91	}92	put(root, 'keep.txt', 'keep\n')!93	put(root, 'nested/deep/file.txt', 'deep\n')!94	put(root, 'skip.log', 'noise\n')!95	put(root, 'build/output.o', 'binary\n')!96	put(root, '.kohignore', '*.log\nbuild/\n')!97	os.symlink('keep.txt', os.join_path(root, 'link.txt')) or {}9899	entries := walk(store, root, load(root))!100	mut paths := []string{}101	for entry in entries {102		paths << entry.path103	}104	assert paths == ['.kohignore', 'keep.txt', 'nested/deep/file.txt']105}106107fn test_walk_records_the_executable_bit() {108	mut store := storage.create(temp_root('exec')!)!109	root := store.root110	defer {111		store.close()112		os.rmdir_all(root) or {}113	}114	put(root, 'run.sh', '#!/bin/sh\necho hi\n')!115	put(root, 'plain.txt', 'hello\n')!116	os.chmod(os.join_path(root, 'run.sh'), 0o755)!117118	mut executable := false119	mut plain_executable := true120	for entry in walk(store, root, Ignore{})! {121		if entry.path == 'run.sh' {122			executable = entry.executable123		}124		if entry.path == 'plain.txt' {125			plain_executable = entry.executable126		}127	}128	assert executable129	assert !plain_executable130}131132fn test_legal_path_is_the_shared_save_path_rule() {133	for path in ['src/main.v', 'café/猫.txt', '.kohignore'] {134		assert legal_path(path), path135	}136	for path in ['', '/etc/passwd', 'a//b', './a', 'a/../b', 'a\\b', 'Icon\r', 'a\u0080b',137		[u8(0xff)].bytestr()] {138		assert !legal_path(path), path139	}140}141142// Only bytes outside a valid UTF-8 sequence become \xNN; Kepr's escapePath prints the same.143fn test_escape_path_escapes_only_the_invalid_bytes() {144	assert escape_path('é' + [u8(0xff)].bytestr() + 'é') == 'é\\xffé'145	assert escape_path([u8(0xed), 0xa0, 0x80].bytestr()) == '\\xed\\xa0\\x80'146	assert escape_path('a\u0085\\b\r') == 'a\\u0085\\\\b\\x0d'147}148149fn test_walk_skips_and_reports_unsafe_names() {150	root := temp_root('unsafe_names')!151	defer {152		os.rmdir_all(root) or {}153	}154	store := storage.create(root)!155	defer {156		store.close()157	}158	put(root, 'safe.txt', 'kept\n')!159	os.write_file('${root}/bad\\name.txt', 'skipped\n')!160	os.write_file('${root}/Icon\r', 'skipped\n')!161	walked := walk_report(store, root, Ignore{})!162	assert walked.files.map(it.path) == ['safe.txt']163	assert walked.unsafe_paths == ['Icon\\x0d', 'bad\\\\name.txt']164}165166fn test_write_tree_refuses_every_path_outside_the_root_before_writing() {167	base := temp_root('restore_containment')!168	defer {169		os.rmdir_all(base) or {}170	}171	root := os.join_path(base, 'repo')172	os.mkdir(root)!173	store := storage.create(root)!174	defer {175		store.close()176	}177	for path in ['../outside.txt', '/etc/koh-test', 'a/../../outside.txt', 'bad\\name', 'control\rname'] {178		if _ := write_tree(store, root, Ignore{}, [FileEntry{179			path: path180		}], []) {181			assert false, '${path} must be refused'182		}183	}184	assert !os.exists(os.join_path(base, 'outside.txt'))185}186187fn test_prune_empty_dirs_never_touches_a_sibling_with_the_roots_prefix() {188	base := temp_root('prune_prefix')!189	defer {190		os.rmdir_all(base) or {}191	}192	root := os.join_path(base, 'repo')193	sibling := os.join_path(base, 'repo2')194	os.mkdir(root)!195	os.mkdir(sibling)!196	prune_empty_dirs(root, sibling)197	assert os.is_dir(sibling)198}199200// The point of nanosecond timestamps: a file rewritten inside the same second201// with the same size must not reuse the cached hash, or a save would record202// content that is no longer on disk.203fn test_cache_does_not_reuse_a_same_size_same_second_edit() {204	mut store := storage.create(temp_root('cache')!)!205	root := store.root206	defer {207		store.close()208		os.rmdir_all(root) or {}209	}210	put(root, 'file.txt', 'aaaa\n')!211	first := walk(store, root, Ignore{})!212	assert first[0].hash == storage.hash_hex('aaaa\n'.bytes())213214	put(root, 'file.txt', 'bbbb\n')!215	second := walk(store, root, Ignore{})!216	assert second[0].hash == storage.hash_hex('bbbb\n'.bytes())217	assert second[0].hash != first[0].hash218}219220// Linux stamps files from a clock that ticks every few milliseconds, so a221// same-size rewrite in the tick the file was hashed in leaves size, mtime and222// ctime unchanged. A hash taken that close to the last change is never cached;223// the next walk hashes again. Tools such as `cp -p` keep an old mtime while224// rewriting, so the newer of the two times decides.225fn test_cache_skips_a_file_changed_moments_ago() {226	second := i64(1_000_000_000)227	now := 100 * second228	old := now - 10 * second229	assert cacheable(Stat{ mtime_ns: old, ctime_ns: old }, now)230	assert !cacheable(Stat{ mtime_ns: now - second, ctime_ns: now - second }, now)231	assert !cacheable(Stat{ mtime_ns: old, ctime_ns: now - second }, now)232233	mut store := storage.create(temp_root('racy')!)!234	root := store.root235	defer {236		store.close()237		os.rmdir_all(root) or {}238	}239	put(root, 'fresh.txt', 'new\n')!240	put(root, 'copied.txt', 'kept mtime\n')!241	hour_ago := time.now().unix() - 3600242	os.utime(os.join_path(root, 'copied.txt'), hour_ago, hour_ago)!243	walk(store, root, Ignore{})!244	assert store.load_cache()!.len == 0245}246247// `status` must work while another agent's save holds the write lock: the248// cache only saves time, so a busy repo hashes without caching, at once.249fn test_a_walk_does_not_wait_for_another_writer() {250	mut store := storage.create(temp_root('busy_walk')!)!251	root := store.root252	defer {253		store.close()254		os.rmdir_all(root) or {}255	}256	put(root, 'a.txt', 'a\n')!257	// Only a file whose last change is older than the racy window is cached,258	// and ctime cannot be set back, so the test waits it out.259	time.sleep(racy_window_ns * time.nanosecond + 100 * time.millisecond)260	writer := storage.open(root)!261	defer {262		writer.close()263	}264	writer.begin()!265	started := time.now()266	files := walk(store, root, Ignore{})!267	assert files.len == 1268	assert time.since(started) < time.second269	writer.rollback()270}271272fn test_restore_writes_removes_and_leaves_ignored_files_alone() {273	mut store := storage.create(temp_root('restore')!)!274	root := store.root275	defer {276		store.close()277		os.rmdir_all(root) or {}278	}279	put(root, 'keep.txt', 'recorded\n')!280	put(root, 'gone/inside.txt', 'also recorded\n')!281	put(root, 'noise.log', 'ignored, must survive\n')!282	put(root, '.kohignore', '*.log\n')!283	target := walk(store, root, load(root))!284	store_objects(store, root, target)!285286	// Drift the working tree: clobber one file, delete another, and add a file287	// the recorded tree has never seen.288	os.write_file(os.join_path(root, 'keep.txt'), 'clobbered\n')!289	os.rm(full_path(root, 'gone/inside.txt'))!290	put(root, 'extra.txt', 'never saved\n')!291292	// The removal list names an ignored file (which survives), a file the293	// target still has (rewritten, not removed), and one genuinely extra file.294	report := restore_files(store, root, load(root), target,295		['extra.txt', '.kohignore', 'noise.log'])!296297	// The clobbered and the deleted file are written; the unchanged298	// .kohignore is left as it is.299	assert report.written == 2300	assert report.removed == 1301	assert os.read_file(full_path(root, 'keep.txt'))! == 'recorded\n'302	assert os.read_file(full_path(root, 'gone/inside.txt'))! == 'also recorded\n'303	assert !os.exists(full_path(root, 'extra.txt'))304	assert os.read_file(full_path(root, 'noise.log'))! == 'ignored, must survive\n'305	assert os.read_file(full_path(root, '.kohignore'))! == '*.log\n'306}307308fn test_restore_keeps_the_executable_bit() {309	mut store := storage.create(temp_root('restore_exec')!)!310	root := store.root311	defer {312		store.close()313		os.rmdir_all(root) or {}314	}315	put(root, 'run.sh', '#!/bin/sh\necho hi\n')!316	os.chmod(os.join_path(root, 'run.sh'), 0o755)!317	target := walk(store, root, Ignore{})!318	store_objects(store, root, target)!319	os.chmod(os.join_path(root, 'run.sh'), 0o644)!320321	restore_files(store, root, Ignore{}, target, [])!322	mode := (lstat(os.join_path(root, 'run.sh')) or { panic('missing') }).mode323	assert is_executable(mode)324}325326fn test_diff_reports_kinds_counts_and_binary_files() {327	mut store := storage.create(temp_root('diff')!)!328	root := store.root329	defer {330		store.close()331		os.rmdir_all(root) or {}332	}333	put(root, 'text.txt', 'one\ntwo\n')!334	put(root, 'same.txt', 'unchanged\n')!335	put(root, 'gone.txt', 'bye\n')!336	before := walk(store, root, Ignore{})!337	store_objects(store, root, before)!338339	os.write_file(os.join_path(root, 'text.txt'), 'one\nthree\nfour\n')!340	os.rm(full_path(root, 'gone.txt'))!341	os.write_file(os.join_path(root, 'added.txt'), 'new\n')!342	os.write_bytes(os.join_path(root, 'blob.bin'), [u8(0), 0x01, 0x02])!343	after := walk(store, root, Ignore{})!344345	// A save's side of a diff is bytes from the object store; only the side346	// that is the working tree reads from disk.347	deltas := diff_trees(store, Tree{348		files: before349	}, Tree{350		root:  root351		files: after352	})!353	mut by_path := map[string]Delta{}354	for delta in deltas {355		by_path[delta.path] = delta356	}357	assert deltas.len == 4358	assert by_path['text.txt'].kind == 'modified'359	assert by_path['text.txt'].added == 2360	assert by_path['text.txt'].removed == 1361	assert by_path['added.txt'].kind == 'added'362	assert by_path['gone.txt'].kind == 'removed'363	assert by_path['blob.bin'].binary364	assert by_path['blob.bin'].patch == binary_notice365	assert 'same.txt' !in by_path366}367368fn full_path(root string, rel string) string {369	return os.join_path(root, rel)370}