Skip to content

Commit bebc6d8

Browse files
committed
Use GIT_AUTHOR/COMMITTER env vars in test_watcher instead of git config
Previous tests ran `cd <tmpdir> && git config user.email test@test && ...` to set up author info for test commits. If the tmpdir path was ever interpreted differently (or if the test process inherited a cwd inside a real git repo), the `git config` call could write to a real repo's .git/config instead of the temp repo's. Replace with `export GIT_AUTHOR_NAME=t GIT_AUTHOR_EMAIL=t@t ...` which are ephemeral env vars that can never leak into persistent config.
1 parent 2c8be91 commit bebc6d8

1 file changed

Lines changed: 46 additions & 44 deletions

File tree

tests/test_watcher.c

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,8 @@ TEST(watcher_detects_git_commit) {
237237

238238
char cmd[512];
239239
snprintf(cmd, sizeof(cmd),
240-
"cd '%s' && git init -q && git config user.email test@test && "
241-
"git config user.name test && echo 'hello' > file.txt && "
240+
"export GIT_AUTHOR_NAME=t GIT_AUTHOR_EMAIL=t@t GIT_COMMITTER_NAME=t "
241+
"GIT_COMMITTER_EMAIL=t@t; cd '%s' &&git init -q &&echo 'hello' > file.txt && "
242242
"git add file.txt && git commit -q -m 'init'",
243243
tmpdir);
244244
if (system(cmd) != 0) {
@@ -258,7 +258,8 @@ TEST(watcher_detects_git_commit) {
258258

259259
/* Make a change: new commit */
260260
snprintf(cmd, sizeof(cmd),
261-
"cd '%s' && echo 'world' >> file.txt && "
261+
"export GIT_AUTHOR_NAME=t GIT_AUTHOR_EMAIL=t@t GIT_COMMITTER_NAME=t "
262+
"GIT_COMMITTER_EMAIL=t@t; cd '%s' &&echo 'world' >> file.txt && "
262263
"git add file.txt && git commit -q -m 'add world'",
263264
tmpdir);
264265
system(cmd);
@@ -289,8 +290,8 @@ TEST(watcher_detects_dirty_worktree) {
289290

290291
char cmd[512];
291292
snprintf(cmd, sizeof(cmd),
292-
"cd '%s' && git init -q && git config user.email test@test && "
293-
"git config user.name test && echo 'hello' > file.txt && "
293+
"export GIT_AUTHOR_NAME=t GIT_AUTHOR_EMAIL=t@t GIT_COMMITTER_NAME=t "
294+
"GIT_COMMITTER_EMAIL=t@t; cd '%s' &&git init -q &&echo 'hello' > file.txt && "
294295
"git add file.txt && git commit -q -m 'init'",
295296
tmpdir);
296297
if (system(cmd) != 0) {
@@ -335,8 +336,8 @@ TEST(watcher_detects_new_file) {
335336

336337
char cmd[512];
337338
snprintf(cmd, sizeof(cmd),
338-
"cd '%s' && git init -q && git config user.email test@test && "
339-
"git config user.name test && echo 'hello' > file.txt && "
339+
"export GIT_AUTHOR_NAME=t GIT_AUTHOR_EMAIL=t@t GIT_COMMITTER_NAME=t "
340+
"GIT_COMMITTER_EMAIL=t@t; cd '%s' &&git init -q &&echo 'hello' > file.txt && "
340341
"git add file.txt && git commit -q -m 'init'",
341342
tmpdir);
342343
if (system(cmd) != 0) {
@@ -382,8 +383,8 @@ TEST(watcher_no_change_no_reindex) {
382383

383384
char cmd[512];
384385
snprintf(cmd, sizeof(cmd),
385-
"cd '%s' && git init -q && git config user.email test@test && "
386-
"git config user.name test && echo 'hello' > file.txt && "
386+
"export GIT_AUTHOR_NAME=t GIT_AUTHOR_EMAIL=t@t GIT_COMMITTER_NAME=t "
387+
"GIT_COMMITTER_EMAIL=t@t; cd '%s' &&git init -q &&echo 'hello' > file.txt && "
387388
"git add file.txt && git commit -q -m 'init'",
388389
tmpdir);
389390
if (system(cmd) != 0) {
@@ -426,17 +427,17 @@ TEST(watcher_multiple_projects) {
426427

427428
char cmd[512];
428429
snprintf(cmd, sizeof(cmd),
429-
"cd '%s' && git init -q && git config user.email test@test && "
430-
"git config user.name test && echo 'a' > a.txt && "
430+
"export GIT_AUTHOR_NAME=t GIT_AUTHOR_EMAIL=t@t GIT_COMMITTER_NAME=t "
431+
"GIT_COMMITTER_EMAIL=t@t; cd '%s' &&git init -q &&echo 'a' > a.txt && "
431432
"git add a.txt && git commit -q -m 'init'",
432433
tmpdirA);
433434
if (system(cmd) != 0) {
434435
SKIP("git not available");
435436
}
436437

437438
snprintf(cmd, sizeof(cmd),
438-
"cd '%s' && git init -q && git config user.email test@test && "
439-
"git config user.name test && echo 'b' > b.txt && "
439+
"export GIT_AUTHOR_NAME=t GIT_AUTHOR_EMAIL=t@t GIT_COMMITTER_NAME=t "
440+
"GIT_COMMITTER_EMAIL=t@t; cd '%s' &&git init -q &&echo 'b' > b.txt && "
440441
"git add b.txt && git commit -q -m 'init'",
441442
tmpdirB);
442443
if (system(cmd) != 0) {
@@ -547,8 +548,8 @@ TEST(watcher_interval_blocks_repoll) {
547548

548549
char cmd[512];
549550
snprintf(cmd, sizeof(cmd),
550-
"cd '%s' && git init -q && git config user.email test@test && "
551-
"git config user.name test && echo 'hello' > file.txt && "
551+
"export GIT_AUTHOR_NAME=t GIT_AUTHOR_EMAIL=t@t GIT_COMMITTER_NAME=t "
552+
"GIT_COMMITTER_EMAIL=t@t; cd '%s' &&git init -q &&echo 'hello' > file.txt && "
552553
"git add file.txt && git commit -q -m 'init'",
553554
tmpdir);
554555
if (system(cmd) != 0) {
@@ -623,8 +624,8 @@ TEST(watcher_git_removed_no_crash) {
623624

624625
char cmd[512];
625626
snprintf(cmd, sizeof(cmd),
626-
"cd '%s' && git init -q && git config user.email test@test && "
627-
"git config user.name test && echo 'hello' > file.txt && "
627+
"export GIT_AUTHOR_NAME=t GIT_AUTHOR_EMAIL=t@t GIT_COMMITTER_NAME=t "
628+
"GIT_COMMITTER_EMAIL=t@t; cd '%s' &&git init -q &&echo 'hello' > file.txt && "
628629
"git add file.txt && git commit -q -m 'init'",
629630
tmpdir);
630631
if (system(cmd) != 0) {
@@ -670,8 +671,8 @@ TEST(watcher_continued_dirty) {
670671

671672
char cmd[512];
672673
snprintf(cmd, sizeof(cmd),
673-
"cd '%s' && git init -q && git config user.email test@test && "
674-
"git config user.name test && echo 'hello' > file.txt && "
674+
"export GIT_AUTHOR_NAME=t GIT_AUTHOR_EMAIL=t@t GIT_COMMITTER_NAME=t "
675+
"GIT_COMMITTER_EMAIL=t@t; cd '%s' &&git init -q &&echo 'hello' > file.txt && "
675676
"git add file.txt && git commit -q -m 'init'",
676677
tmpdir);
677678
if (system(cmd) != 0) {
@@ -706,7 +707,8 @@ TEST(watcher_continued_dirty) {
706707
ASSERT_EQ(index_call_count, 2);
707708

708709
/* Commit to clean up, then poll — should not trigger */
709-
snprintf(cmd, sizeof(cmd), "cd '%s' && git add file.txt && git commit -q -m 'clean'", tmpdir);
710+
snprintf(cmd, sizeof(cmd), "export GIT_AUTHOR_NAME=t GIT_AUTHOR_EMAIL=t@t GIT_COMMITTER_NAME=t "
711+
"GIT_COMMITTER_EMAIL=t@t; cd '%s' &&git add file.txt && git commit -q -m 'clean'", tmpdir);
710712
system(cmd);
711713

712714
/* HEAD changed → will trigger one more reindex */
@@ -740,8 +742,8 @@ TEST(watcher_baseline_dirty_repo) {
740742

741743
char cmd[512];
742744
snprintf(cmd, sizeof(cmd),
743-
"cd '%s' && git init -q && git config user.email test@test && "
744-
"git config user.name test && echo 'hello' > file.txt && "
745+
"export GIT_AUTHOR_NAME=t GIT_AUTHOR_EMAIL=t@t GIT_COMMITTER_NAME=t "
746+
"GIT_COMMITTER_EMAIL=t@t; cd '%s' &&git init -q &&echo 'hello' > file.txt && "
745747
"git add file.txt && git commit -q -m 'init'",
746748
tmpdir);
747749
if (system(cmd) != 0) {
@@ -786,8 +788,8 @@ TEST(watcher_unwatch_prunes_state) {
786788

787789
char cmd[512];
788790
snprintf(cmd, sizeof(cmd),
789-
"cd '%s' && git init -q && git config user.email test@test && "
790-
"git config user.name test && echo 'hello' > file.txt && "
791+
"export GIT_AUTHOR_NAME=t GIT_AUTHOR_EMAIL=t@t GIT_COMMITTER_NAME=t "
792+
"GIT_COMMITTER_EMAIL=t@t; cd '%s' &&git init -q &&echo 'hello' > file.txt && "
791793
"git add file.txt && git commit -q -m 'init'",
792794
tmpdir);
793795
if (system(cmd) != 0) {
@@ -834,8 +836,8 @@ TEST(watcher_watch_after_unwatch) {
834836

835837
char cmd[512];
836838
snprintf(cmd, sizeof(cmd),
837-
"cd '%s' && git init -q && git config user.email test@test && "
838-
"git config user.name test && echo 'hello' > file.txt && "
839+
"export GIT_AUTHOR_NAME=t GIT_AUTHOR_EMAIL=t@t GIT_COMMITTER_NAME=t "
840+
"GIT_COMMITTER_EMAIL=t@t; cd '%s' &&git init -q &&echo 'hello' > file.txt && "
839841
"git add file.txt && git commit -q -m 'init'",
840842
tmpdir);
841843
if (system(cmd) != 0) {
@@ -897,8 +899,8 @@ TEST(watcher_detects_file_delete) {
897899

898900
char cmd[512];
899901
snprintf(cmd, sizeof(cmd),
900-
"cd '%s' && git init -q && git config user.email test@test && "
901-
"git config user.name test && echo 'hello' > file.txt && "
902+
"export GIT_AUTHOR_NAME=t GIT_AUTHOR_EMAIL=t@t GIT_COMMITTER_NAME=t "
903+
"GIT_COMMITTER_EMAIL=t@t; cd '%s' &&git init -q &&echo 'hello' > file.txt && "
902904
"echo 'todelete' > todelete.go && "
903905
"git add -A && git commit -q -m 'init'",
904906
tmpdir);
@@ -944,8 +946,8 @@ TEST(watcher_detects_subdir_file) {
944946

945947
char cmd[512];
946948
snprintf(cmd, sizeof(cmd),
947-
"cd '%s' && git init -q && git config user.email test@test && "
948-
"git config user.name test && echo 'hello' > main.go && "
949+
"export GIT_AUTHOR_NAME=t GIT_AUTHOR_EMAIL=t@t GIT_COMMITTER_NAME=t "
950+
"GIT_COMMITTER_EMAIL=t@t; cd '%s' &&git init -q &&echo 'hello' > main.go && "
949951
"git add main.go && git commit -q -m 'init'",
950952
tmpdir);
951953
if (system(cmd) != 0) {
@@ -1016,8 +1018,8 @@ TEST(watcher_full_flow_new_file) {
10161018

10171019
char cmd[512];
10181020
snprintf(cmd, sizeof(cmd),
1019-
"cd '%s' && git init -q && git config user.email test@test && "
1020-
"git config user.name test && echo 'package main' > main.go && "
1021+
"export GIT_AUTHOR_NAME=t GIT_AUTHOR_EMAIL=t@t GIT_COMMITTER_NAME=t "
1022+
"GIT_COMMITTER_EMAIL=t@t; cd '%s' &&git init -q &&echo 'package main' > main.go && "
10211023
"git add main.go && git commit -q -m 'init'",
10221024
tmpdir);
10231025
if (system(cmd) != 0) {
@@ -1068,8 +1070,8 @@ TEST(watcher_fallback_still_detects) {
10681070

10691071
char cmd[512];
10701072
snprintf(cmd, sizeof(cmd),
1071-
"cd '%s' && git init -q && git config user.email test@test && "
1072-
"git config user.name test && echo 'hello' > main.go && "
1073+
"export GIT_AUTHOR_NAME=t GIT_AUTHOR_EMAIL=t@t GIT_COMMITTER_NAME=t "
1074+
"GIT_COMMITTER_EMAIL=t@t; cd '%s' &&git init -q &&echo 'hello' > main.go && "
10731075
"git add main.go && git commit -q -m 'init'",
10741076
tmpdir);
10751077
if (system(cmd) != 0) {
@@ -1131,17 +1133,17 @@ TEST(watcher_poll_only_watched_projects) {
11311133
char cmd[512];
11321134
/* Init both repos */
11331135
snprintf(cmd, sizeof(cmd),
1134-
"cd '%s' && git init -q && git config user.email test@test && "
1135-
"git config user.name test && echo 'a' > a.txt && "
1136+
"export GIT_AUTHOR_NAME=t GIT_AUTHOR_EMAIL=t@t GIT_COMMITTER_NAME=t "
1137+
"GIT_COMMITTER_EMAIL=t@t; cd '%s' &&git init -q &&echo 'a' > a.txt && "
11361138
"git add a.txt && git commit -q -m 'init'",
11371139
tmpdirA);
11381140
if (system(cmd) != 0) {
11391141
SKIP("git not available");
11401142
}
11411143

11421144
snprintf(cmd, sizeof(cmd),
1143-
"cd '%s' && git init -q && git config user.email test@test && "
1144-
"git config user.name test && echo 'b' > b.txt && "
1145+
"export GIT_AUTHOR_NAME=t GIT_AUTHOR_EMAIL=t@t GIT_COMMITTER_NAME=t "
1146+
"GIT_COMMITTER_EMAIL=t@t; cd '%s' &&git init -q &&echo 'b' > b.txt && "
11451147
"git add b.txt && git commit -q -m 'init'",
11461148
tmpdirB);
11471149
if (system(cmd) != 0) {
@@ -1195,8 +1197,8 @@ TEST(watcher_touch_resets_immediate) {
11951197

11961198
char cmd[512];
11971199
snprintf(cmd, sizeof(cmd),
1198-
"cd '%s' && git init -q && git config user.email test@test && "
1199-
"git config user.name test && echo 'hello' > file.txt && "
1200+
"export GIT_AUTHOR_NAME=t GIT_AUTHOR_EMAIL=t@t GIT_COMMITTER_NAME=t "
1201+
"GIT_COMMITTER_EMAIL=t@t; cd '%s' &&git init -q &&echo 'hello' > file.txt && "
12001202
"git add file.txt && git commit -q -m 'init'",
12011203
tmpdir);
12021204
if (system(cmd) != 0) {
@@ -1247,8 +1249,8 @@ TEST(watcher_modify_tracked_file) {
12471249

12481250
char cmd[512];
12491251
snprintf(cmd, sizeof(cmd),
1250-
"cd '%s' && git init -q && git config user.email test@test && "
1251-
"git config user.name test && echo 'package main' > main.go && "
1252+
"export GIT_AUTHOR_NAME=t GIT_AUTHOR_EMAIL=t@t GIT_COMMITTER_NAME=t "
1253+
"GIT_COMMITTER_EMAIL=t@t; cd '%s' &&git init -q &&echo 'package main' > main.go && "
12521254
"git add main.go && git commit -q -m 'init'",
12531255
tmpdir);
12541256
if (system(cmd) != 0) {
@@ -1543,8 +1545,8 @@ TEST(watcher_callback_data_passed) {
15431545

15441546
char cmd[512];
15451547
snprintf(cmd, sizeof(cmd),
1546-
"cd '%s' && git init -q && git config user.email test@test && "
1547-
"git config user.name test && echo 'hello' > file.txt && "
1548+
"export GIT_AUTHOR_NAME=t GIT_AUTHOR_EMAIL=t@t GIT_COMMITTER_NAME=t "
1549+
"GIT_COMMITTER_EMAIL=t@t; cd '%s' &&git init -q &&echo 'hello' > file.txt && "
15481550
"git add file.txt && git commit -q -m 'init'",
15491551
tmpdir);
15501552
if (system(cmd) != 0) {

0 commit comments

Comments
 (0)