From 7d16dae4f4237b70e1257c6a3aafb1678303b4e2 Mon Sep 17 00:00:00 2001 From: Spencer Brower Date: Fri, 12 Jun 2026 13:42:37 -0400 Subject: [PATCH] refactor: Fixed the rest of the (tested) leaks. --- cli_test.odin | 4 ++-- cmd_check_test.odin | 10 +++++----- cmd_list_test.odin | 16 ++++------------ db.odin | 6 +++--- db_test.odin | 19 ------------------- scan.odin | 2 +- scan_test.odin | 16 ++++++++-------- table.odin | 4 ++-- table_test.odin | 28 ++++++++++++++++------------ 9 files changed, 41 insertions(+), 64 deletions(-) delete mode 100644 db_test.odin diff --git a/cli_test.odin b/cli_test.odin index a28ef73..aed5400 100644 --- a/cli_test.odin +++ b/cli_test.odin @@ -17,10 +17,10 @@ test_usage_text_contains_all_commands :: proc(t: ^testing.T) { testing.expect( t, strings.contains(text, c.name), - fmt.aprintf("usage missing command %q", c.name), + fmt.tprintf("usage missing command %q", c.name), ) for a in c.aliases { - testing.expect(t, strings.contains(text, a), fmt.aprintf("usage missing alias %q", a)) + testing.expect(t, strings.contains(text, a), fmt.tprintf("usage missing alias %q", a)) } } } diff --git a/cmd_check_test.odin b/cmd_check_test.odin index 21c6b35..591d9dc 100644 --- a/cmd_check_test.odin +++ b/cmd_check_test.odin @@ -9,12 +9,12 @@ test_find_unbacked_finds_missing :: proc(t: ^testing.T) { db := []EnvFile{{Path = "/a/.env"}, {Path = "/b/.env"}} result := find_unbacked(local, db[:]) - testing.expect(t, len(result) == 1, fmt.aprintf("expected 1 unbacked, got %d", len(result))) + testing.expect(t, len(result) == 1, fmt.tprintf("expected 1 unbacked, got %d", len(result))) if len(result) > 0 { testing.expect( t, result[0] == "/c/.env", - fmt.aprintf("expected /c/.env, got %s", result[0]), + fmt.tprintf("expected /c/.env, got %s", result[0]), ) } } @@ -25,7 +25,7 @@ test_find_unbacked_all_backed :: proc(t: ^testing.T) { db := []EnvFile{{Path = "/a/.env"}, {Path = "/b/.env"}} result := find_unbacked(local, db[:]) - testing.expect(t, len(result) == 0, fmt.aprintf("expected 0 unbacked, got %d", len(result))) + testing.expect(t, len(result) == 0, fmt.tprintf("expected 0 unbacked, got %d", len(result))) } @(test) @@ -34,7 +34,7 @@ test_find_unbacked_no_local :: proc(t: ^testing.T) { db := []EnvFile{{Path = "/a/.env"}} result := find_unbacked(local, db[:]) - testing.expect(t, len(result) == 0, fmt.aprintf("expected 0 unbacked, got %d", len(result))) + testing.expect(t, len(result) == 0, fmt.tprintf("expected 0 unbacked, got %d", len(result))) } @(test) @@ -43,6 +43,6 @@ test_find_unbacked_none_backed :: proc(t: ^testing.T) { db: []EnvFile result := find_unbacked(local, db[:]) - testing.expect(t, len(result) == 2, fmt.aprintf("expected 2 unbacked, got %d", len(result))) + testing.expect(t, len(result) == 2, fmt.tprintf("expected 2 unbacked, got %d", len(result))) } diff --git a/cmd_list_test.odin b/cmd_list_test.odin index b1458e9..2c34f9b 100644 --- a/cmd_list_test.odin +++ b/cmd_list_test.odin @@ -5,22 +5,14 @@ import "core:testing" @(test) test_filepath_base_equals_rel :: proc(t: ^testing.T) { - cases := []string{ - "/home/user/.env", - "/home/user/project/.envrc", - "/tmp/foo", - "/a/b/c/d.txt", - } + cases := []string{"/home/user/.env", "/home/user/project/.envrc", "/tmp/foo", "/a/b/c/d.txt"} for path in cases { dir := filepath.dir(path) - rel, rel_err := filepath.rel(dir, path) + rel, rel_err := filepath.rel(dir, path, context.temp_allocator) testing.expect(t, rel_err == nil, "filepath.rel returned an error") base := filepath.base(path) - testing.expect( - t, - rel == base, - "filepath.rel(dir, path) should equal filepath.base(path)", - ) + testing.expect(t, rel == base, "filepath.rel(dir, path) should equal filepath.base(path)") } } + diff --git a/db.odin b/db.odin index 1750b3f..a6c117a 100644 --- a/db.odin +++ b/db.odin @@ -577,7 +577,7 @@ env_file_sync :: proc(f: ^EnvFile, dir: SyncDirection, d: ^Db) -> (SyncResult, s if file_stat_err != nil { write_err := os.write_entire_file(f.Path, f.contents) if write_err != nil { - msg, _ := strings.concatenate({"failed to write file: ", fmt.aprintf("%v", write_err)}) + msg, _ := strings.concatenate({"failed to write file: ", fmt.tprintf("%v", write_err)}) return .Error, msg } @@ -587,7 +587,7 @@ env_file_sync :: proc(f: ^EnvFile, dir: SyncDirection, d: ^Db) -> (SyncResult, s data, read_err := os.read_entire_file_from_path(f.Path, context.allocator) if read_err != nil { - msg, _ := strings.concatenate({"failed to read file for SHA comparison: ", fmt.aprintf("%v", read_err)}) + msg, _ := strings.concatenate({"failed to read file for SHA comparison: ", fmt.tprintf("%v", read_err)}) return .Error, msg } @@ -603,7 +603,7 @@ env_file_sync :: proc(f: ^EnvFile, dir: SyncDirection, d: ^Db) -> (SyncResult, s case .TrustDatabase: write_err := os.write_entire_file(f.Path, f.contents) if write_err != nil { - msg, _ := strings.concatenate({"failed to write file: ", fmt.aprintf("%v", write_err)}) + msg, _ := strings.concatenate({"failed to write file: ", fmt.tprintf("%v", write_err)}) return .Error, msg } s := i32(result) | i32(SyncResult.Restored) diff --git a/db_test.odin b/db_test.odin deleted file mode 100644 index 8565b7c..0000000 --- a/db_test.odin +++ /dev/null @@ -1,19 +0,0 @@ -package main - -import "core:path/filepath" -import "core:strings" -import "core:testing" - -@(test) -test_dir_slice_owns_parent :: proc(t: ^testing.T) { - abs_path := "/home/user/project/.env" - cloned_path, _ := strings.clone(abs_path) - - dir := filepath.dir(cloned_path) - - testing.expect(t, dir == "/home/user/project", "filepath.dir should return parent directory") - testing.expect(t, len(dir) > 0, "dir should not be empty") - - cloned_dir, _ := strings.clone(dir) - testing.expect(t, cloned_dir == dir, "clone of dir should equal dir") -} diff --git a/scan.odin b/scan.odin index 53a60a0..1dd49d2 100644 --- a/scan.odin +++ b/scan.odin @@ -121,7 +121,7 @@ next_fd_tmp_path :: proc() -> string { n := fd_seq fd_seq += 1 sync.atomic_mutex_unlock(&fd_counter) - return fmt.aprintf("/tmp/envr-fd-%d-%d", os.get_pid(), n, allocator = context.temp_allocator) + return fmt.tprintf("/tmp/envr-fd-%d-%d", os.get_pid(), n) } cant_scan :: proc(feats: AvailableFeatures) -> bool { diff --git a/scan_test.odin b/scan_test.odin index 49880bb..535d2df 100644 --- a/scan_test.odin +++ b/scan_test.odin @@ -10,12 +10,12 @@ test_scan_path_finds_gitignored_env_files :: proc(t: ^testing.T) { feats := check_features() testing.expect(t, cant_scan(feats) == false) - base := fmt.aprintf("/tmp/envr-scan-test-%d", os.get_pid()) + base := fmt.tprintf("/tmp/envr-scan-test-%d", os.get_pid()) os.mkdir_all(base) defer os.remove_all(base) git_init := os.Process_Desc { - command = []string{"git", "-c", "advice.defaultBranchName=false", "init"}, + command = []string{"git", "-c", "advice.defaultBranchName=false", "init", "-q"}, working_dir = base, stdout = os.stderr, stderr = os.stderr, @@ -29,12 +29,12 @@ test_scan_path_finds_gitignored_env_files :: proc(t: ^testing.T) { return } - gitignore_path := fmt.aprintf("%s/.gitignore", base) + gitignore_path := fmt.tprintf("%s/.gitignore", base) _ = os.write_entire_file(gitignore_path, ".env*\n") - _ = os.write_entire_file(fmt.aprintf("%s/.env", base), "SECRET=1") - _ = os.write_entire_file(fmt.aprintf("%s/.env.testing", base), "TEST=1") - _ = os.write_entire_file(fmt.aprintf("%s/config.yaml", base), "key: value") + _ = os.write_entire_file(fmt.tprintf("%s/.env", base), "SECRET=1") + _ = os.write_entire_file(fmt.tprintf("%s/.env.testing", base), "TEST=1") + _ = os.write_entire_file(fmt.tprintf("%s/config.yaml", base), "key: value") cfg := Config { ScanConfig = ScanConfig{Matcher = "\\.env", Exclude = []string{}, Include = []string{}}, @@ -71,7 +71,7 @@ test_scan_path_empty_dir :: proc(t: ^testing.T) { feats := check_features() testing.expect(t, cant_scan(feats) == false) - base := fmt.aprintf("/tmp/envr-scan-empty-%d", os.get_pid()) + base := fmt.tprintf("/tmp/envr-scan-empty-%d", os.get_pid()) os.mkdir_all(base) defer os.remove_all(base) @@ -82,6 +82,6 @@ test_scan_path_empty_dir :: proc(t: ^testing.T) { results, ok := scan_path(base, cfg) defer delete(results) testing.expect(t, ok, "scan_path should succeed") - testing.expect(t, len(results) == 0, fmt.aprintf("expected 0 results, got %d", len(results))) + testing.expect(t, len(results) == 0, fmt.tprintf("expected 0 results, got %d", len(results))) } diff --git a/table.odin b/table.odin index 5fab02b..0f45388 100644 --- a/table.odin +++ b/table.odin @@ -87,11 +87,11 @@ render_json_rows :: proc(w: io.Writer, headers: []string, rows: [][]string) { append(&entries, entry) } - data, err := json.marshal(entries[:]) + data, err := json.marshal(entries[:], allocator = context.temp_allocator) if err != nil { fmt.eprintf("Error marshaling JSON: %v\n", err) return } - io.write_string(w, string(data)) + fmt.wprintf(w, "%s", data, flush = false) } diff --git a/table_test.odin b/table_test.odin index c884412..107c206 100644 --- a/table_test.odin +++ b/table_test.odin @@ -20,18 +20,18 @@ test_render_json_rows_normal :: proc(t: ^testing.T) { output := strings.to_string(b) - result: []map[string]string - unmarshal_err := json.unmarshal_string(output, &result) + result: []map[string]string = --- + unmarshal_err := json.unmarshal_string(output, &result, allocator = context.temp_allocator) testing.expect( t, unmarshal_err == nil, - fmt.aprintf("json unmarshal failed: %v\noutput was: %q", unmarshal_err, output), + fmt.tprintf("json unmarshal failed: %v\noutput was: %q", unmarshal_err, output), ) - testing.expect(t, len(result) == 2, fmt.aprintf("expected 2 rows, got %d", len(result))) + testing.expect(t, len(result) == 2, fmt.tprintf("expected 2 rows, got %d", len(result))) testing.expect( t, result[0]["name"] == "foo", - fmt.aprintf("expected name=foo, got %q", result[0]["name"]), + fmt.tprintf("expected name=foo, got %q", result[0]["name"]), ) testing.expect(t, result[0]["path"] == "/home/user/.env") testing.expect(t, result[1]["name"] == "bar") @@ -57,18 +57,22 @@ test_render_json_rows_special_chars :: proc(t: ^testing.T) { output := strings.to_string(b) - result: []map[string]string - unmarshal_err := json.unmarshal(transmute([]byte)output, &result) + result: []map[string]string = --- + unmarshal_err := json.unmarshal( + transmute([]byte)output, + &result, + allocator = context.temp_allocator, + ) testing.expect( t, unmarshal_err == nil, - fmt.aprintf("json unmarshal failed: %v\noutput was: %q", unmarshal_err, output), + fmt.tprintf("json unmarshal failed: %v\noutput was: %q", unmarshal_err, output), ) testing.expect(t, len(result) == 4) testing.expect( t, result[0]["value"] == `has "double quotes"`, - fmt.aprintf("got %q", result[0]["value"]), + fmt.tprintf("got %q", result[0]["value"]), ) testing.expect(t, result[1]["value"] == `path\to\file`) testing.expect(t, result[2]["value"] == "line1\nline2") @@ -89,12 +93,12 @@ test_render_json_rows_empty :: proc(t: ^testing.T) { output := strings.to_string(b) - result: []map[string]string - unmarshal_err := json.unmarshal_string(output, &result) + result: []map[string]string = --- + unmarshal_err := json.unmarshal_string(output, &result, allocator = context.temp_allocator) testing.expect( t, unmarshal_err == nil, - fmt.aprintf("json unmarshal failed: %v\noutput was: %q", unmarshal_err, output), + fmt.tprintf("json unmarshal failed: %v\noutput was: %q", unmarshal_err, output), ) testing.expect(t, len(result) == 0) }