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 33 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}45464748fn 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 71 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 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}141142fn test_walk_skips_and_reports_unsafe_names() {143 root := temp_root('unsafe_names')!144 defer {145 os.rmdir_all(root) or {}146 }147 store := storage.create(root)!148 defer {149 store.close()150 }151 put(root, 'safe.txt', 'kept\n')!152 os.write_file('${root}/bad\\name.txt', 'skipped\n')!153 os.write_file('${root}/Icon\r', 'skipped\n')!154 walked := walk_report(store, root, Ignore{})!155 assert walked.files.map(it.path) == ['safe.txt']156 assert walked.unsafe_paths == ['Icon\\x0d', 'bad\\\\name.txt']157}158159160161162fn test_cache_does_not_reuse_a_same_size_same_second_edit() {163 mut store := storage.create(temp_root('cache')!)!164 root := store.root165 defer {166 store.close()167 os.rmdir_all(root) or {}168 }169 put(root, 'file.txt', 'aaaa\n')!170 first := walk(store, root, Ignore{})!171 assert first[0].hash == storage.hash_hex('aaaa\n'.bytes())172173 put(root, 'file.txt', 'bbbb\n')!174 second := walk(store, root, Ignore{})!175 assert second[0].hash == storage.hash_hex('bbbb\n'.bytes())176 assert second[0].hash != first[0].hash177}178179180181182183184fn test_cache_skips_a_file_changed_moments_ago() {185 second := i64(1_000_000_000)186 now := 100 * second187 old := now - 10 * second188 assert cacheable(Stat{ mtime_ns: old, ctime_ns: old }, now)189 assert !cacheable(Stat{ mtime_ns: now - second, ctime_ns: now - second }, now)190 assert !cacheable(Stat{ mtime_ns: old, ctime_ns: now - second }, now)191192 mut store := storage.create(temp_root('racy')!)!193 root := store.root194 defer {195 store.close()196 os.rmdir_all(root) or {}197 }198 put(root, 'fresh.txt', 'new\n')!199 put(root, 'copied.txt', 'kept mtime\n')!200 hour_ago := time.now().unix() - 3600201 os.utime(os.join_path(root, 'copied.txt'), hour_ago, hour_ago)!202 walk(store, root, Ignore{})!203 assert store.load_cache()!.len == 0204}205206207208fn test_a_walk_does_not_wait_for_another_writer() {209 mut store := storage.create(temp_root('busy_walk')!)!210 root := store.root211 defer {212 store.close()213 os.rmdir_all(root) or {}214 }215 put(root, 'a.txt', 'a\n')!216 217 218 time.sleep(racy_window_ns * time.nanosecond + 100 * time.millisecond)219 writer := storage.open(root)!220 defer {221 writer.close()222 }223 writer.begin()!224 started := time.now()225 files := walk(store, root, Ignore{})!226 assert files.len == 1227 assert time.since(started) < time.second228 writer.rollback()229}230231fn test_restore_writes_removes_and_leaves_ignored_files_alone() {232 mut store := storage.create(temp_root('restore')!)!233 root := store.root234 defer {235 store.close()236 os.rmdir_all(root) or {}237 }238 put(root, 'keep.txt', 'recorded\n')!239 put(root, 'gone/inside.txt', 'also recorded\n')!240 put(root, 'noise.log', 'ignored, must survive\n')!241 put(root, '.kohignore', '*.log\n')!242 target := walk(store, root, load(root))!243 store_objects(store, root, target)!244245 246 247 os.write_file(os.join_path(root, 'keep.txt'), 'clobbered\n')!248 os.rm(full_path(root, 'gone/inside.txt'))!249 put(root, 'extra.txt', 'never saved\n')!250251 252 253 report := restore_files(store, root, load(root), target,254 ['extra.txt', '.kohignore', 'noise.log'])!255256 257 258 assert report.written == 2259 assert report.removed == 1260 assert os.read_file(full_path(root, 'keep.txt'))! == 'recorded\n'261 assert os.read_file(full_path(root, 'gone/inside.txt'))! == 'also recorded\n'262 assert !os.exists(full_path(root, 'extra.txt'))263 assert os.read_file(full_path(root, 'noise.log'))! == 'ignored, must survive\n'264 assert os.read_file(full_path(root, '.kohignore'))! == '*.log\n'265}266267fn test_restore_keeps_the_executable_bit() {268 mut store := storage.create(temp_root('restore_exec')!)!269 root := store.root270 defer {271 store.close()272 os.rmdir_all(root) or {}273 }274 put(root, 'run.sh', '#!/bin/sh\necho hi\n')!275 os.chmod(os.join_path(root, 'run.sh'), 0o755)!276 target := walk(store, root, Ignore{})!277 store_objects(store, root, target)!278 os.chmod(os.join_path(root, 'run.sh'), 0o644)!279280 restore_files(store, root, Ignore{}, target, [])!281 mode := (lstat(os.join_path(root, 'run.sh')) or { panic('missing') }).mode282 assert is_executable(mode)283}284285fn test_diff_reports_kinds_counts_and_binary_files() {286 mut store := storage.create(temp_root('diff')!)!287 root := store.root288 defer {289 store.close()290 os.rmdir_all(root) or {}291 }292 put(root, 'text.txt', 'one\ntwo\n')!293 put(root, 'same.txt', 'unchanged\n')!294 put(root, 'gone.txt', 'bye\n')!295 before := walk(store, root, Ignore{})!296 store_objects(store, root, before)!297298 os.write_file(os.join_path(root, 'text.txt'), 'one\nthree\nfour\n')!299 os.rm(full_path(root, 'gone.txt'))!300 os.write_file(os.join_path(root, 'added.txt'), 'new\n')!301 os.write_bytes(os.join_path(root, 'blob.bin'), [u8(0), 0x01, 0x02])!302 after := walk(store, root, Ignore{})!303304 305 306 deltas := diff_trees(store, Tree{307 files: before308 }, Tree{309 root: root310 files: after311 })!312 mut by_path := map[string]Delta{}313 for delta in deltas {314 by_path[delta.path] = delta315 }316 assert deltas.len == 4317 assert by_path['text.txt'].kind == 'modified'318 assert by_path['text.txt'].added == 2319 assert by_path['text.txt'].removed == 1320 assert by_path['added.txt'].kind == 'added'321 assert by_path['gone.txt'].kind == 'removed'322 assert by_path['blob.bin'].binary323 assert by_path['blob.bin'].patch == binary_notice324 assert 'same.txt' !in by_path325}326327fn full_path(root string, rel string) string {328 return os.join_path(root, rel)329}