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}4546fn test_hard_ignores_are_not_overridable() {47 for path in ['.koh', '.koh-wal', '.koh-shm', '.koh-v2', '.koh-v2/snapshots/x.json', '.git',48 '.git/config', 'LOG.md'] {49 assert hard_ignored(path), '${path} should be hard ignored'50 }51 assert !hard_ignored('.kohignore')52 assert !hard_ignored('src/log.md')53 54 55 for path in ['inner/.koh', 'inner/.koh-wal', 'inner/.koh-shm', 'a/b/.git', 'vendored/.git/HEAD',56 'old/.koh-v2/snapshots/x.json', 'koh2repo/.koh/objects/ab/cd'] {57 assert hard_ignored(path), '${path} should be hard ignored'58 }59 assert !hard_ignored('inner/.kohignore')60 assert !hard_ignored('docs/.github/workflow.yml')61 assert !hard_ignored('notes/.koh.md')62 63 ig := parse('')64 assert ig.matches('.koh', false)65 assert !ig.matches('.kohignore', false)66}6768fn test_walk_skips_ignored_files_directories_and_symlinks() {69 mut store := storage.create(temp_root('walk')!)!70 root := store.root71 defer {72 store.close()73 os.rmdir_all(root) or {}74 }75 put(root, 'keep.txt', 'keep\n')!76 put(root, 'nested/deep/file.txt', 'deep\n')!77 put(root, 'skip.log', 'noise\n')!78 put(root, 'build/output.o', 'binary\n')!79 put(root, '.kohignore', '*.log\nbuild/\n')!80 os.symlink('keep.txt', os.join_path(root, 'link.txt')) or {}8182 entries := walk(store, root, load(root))!83 mut paths := []string{}84 for entry in entries {85 paths << entry.path86 }87 assert paths == ['.kohignore', 'keep.txt', 'nested/deep/file.txt']88}8990fn test_walk_records_the_executable_bit() {91 mut store := storage.create(temp_root('exec')!)!92 root := store.root93 defer {94 store.close()95 os.rmdir_all(root) or {}96 }97 put(root, 'run.sh', '#!/bin/sh\necho hi\n')!98 put(root, 'plain.txt', 'hello\n')!99 os.chmod(os.join_path(root, 'run.sh'), 0o755)!100101 mut executable := false102 mut plain_executable := true103 for entry in walk(store, root, Ignore{})! {104 if entry.path == 'run.sh' {105 executable = entry.executable106 }107 if entry.path == 'plain.txt' {108 plain_executable = entry.executable109 }110 }111 assert executable112 assert !plain_executable113}114115116117118fn test_cache_does_not_reuse_a_same_size_same_second_edit() {119 mut store := storage.create(temp_root('cache')!)!120 root := store.root121 defer {122 store.close()123 os.rmdir_all(root) or {}124 }125 put(root, 'file.txt', 'aaaa\n')!126 first := walk(store, root, Ignore{})!127 assert first[0].hash == storage.hash_hex('aaaa\n'.bytes())128129 put(root, 'file.txt', 'bbbb\n')!130 second := walk(store, root, Ignore{})!131 assert second[0].hash == storage.hash_hex('bbbb\n'.bytes())132 assert second[0].hash != first[0].hash133}134135136137138139fn test_cache_skips_a_file_written_moments_ago() {140 mut store := storage.create(temp_root('racy')!)!141 root := store.root142 defer {143 store.close()144 os.rmdir_all(root) or {}145 }146 put(root, 'fresh.txt', 'new\n')!147 put(root, 'old.txt', 'old\n')!148 hour_ago := time.now().unix() - 3600149 os.utime(os.join_path(root, 'old.txt'), hour_ago, hour_ago)!150 walk(store, root, Ignore{})!151 assert store.load_cache()!.keys() == ['old.txt']152}153154fn test_restore_writes_removes_and_leaves_ignored_files_alone() {155 mut store := storage.create(temp_root('restore')!)!156 root := store.root157 defer {158 store.close()159 os.rmdir_all(root) or {}160 }161 put(root, 'keep.txt', 'recorded\n')!162 put(root, 'gone/inside.txt', 'also recorded\n')!163 put(root, 'noise.log', 'ignored, must survive\n')!164 put(root, '.kohignore', '*.log\n')!165 target := walk(store, root, load(root))!166 store_objects(store, root, target)!167168 169 170 os.write_file(os.join_path(root, 'keep.txt'), 'clobbered\n')!171 os.rm(full_path(root, 'gone/inside.txt'))!172 put(root, 'extra.txt', 'never saved\n')!173174 175 176 report := restore_files(store, root, load(root), target,177 ['extra.txt', '.kohignore', 'noise.log'])!178179 180 181 assert report.written == 2182 assert report.removed == 1183 assert os.read_file(full_path(root, 'keep.txt'))! == 'recorded\n'184 assert os.read_file(full_path(root, 'gone/inside.txt'))! == 'also recorded\n'185 assert !os.exists(full_path(root, 'extra.txt'))186 assert os.read_file(full_path(root, 'noise.log'))! == 'ignored, must survive\n'187 assert os.read_file(full_path(root, '.kohignore'))! == '*.log\n'188}189190fn test_restore_keeps_the_executable_bit() {191 mut store := storage.create(temp_root('restore_exec')!)!192 root := store.root193 defer {194 store.close()195 os.rmdir_all(root) or {}196 }197 put(root, 'run.sh', '#!/bin/sh\necho hi\n')!198 os.chmod(os.join_path(root, 'run.sh'), 0o755)!199 target := walk(store, root, Ignore{})!200 store_objects(store, root, target)!201 os.chmod(os.join_path(root, 'run.sh'), 0o644)!202203 restore_files(store, root, Ignore{}, target, [])!204 mode := (lstat(os.join_path(root, 'run.sh')) or { panic('missing') }).mode205 assert is_executable(mode)206}207208fn test_diff_reports_kinds_counts_and_binary_files() {209 mut store := storage.create(temp_root('diff')!)!210 root := store.root211 defer {212 store.close()213 os.rmdir_all(root) or {}214 }215 put(root, 'text.txt', 'one\ntwo\n')!216 put(root, 'same.txt', 'unchanged\n')!217 put(root, 'gone.txt', 'bye\n')!218 before := walk(store, root, Ignore{})!219 store_objects(store, root, before)!220221 os.write_file(os.join_path(root, 'text.txt'), 'one\nthree\nfour\n')!222 os.rm(full_path(root, 'gone.txt'))!223 os.write_file(os.join_path(root, 'added.txt'), 'new\n')!224 os.write_bytes(os.join_path(root, 'blob.bin'), [u8(0), 0x01, 0x02])!225 after := walk(store, root, Ignore{})!226227 228 229 deltas := diff_trees(store, Tree{230 files: before231 }, Tree{232 root: root233 files: after234 })!235 mut by_path := map[string]Delta{}236 for delta in deltas {237 by_path[delta.path] = delta238 }239 assert deltas.len == 4240 assert by_path['text.txt'].kind == 'modified'241 assert by_path['text.txt'].added == 2242 assert by_path['text.txt'].removed == 1243 assert by_path['added.txt'].kind == 'added'244 assert by_path['gone.txt'].kind == 'removed'245 assert by_path['blob.bin'].binary246 assert by_path['blob.bin'].patch == binary_notice247 assert 'same.txt' !in by_path248}249250fn full_path(root string, rel string) string {251 return os.join_path(root, rel)252}