99import io .github .cowwoc .cat .claude .hook .BashHandler ;
1010
1111import io .github .cowwoc .cat .claude .hook .bash .BlockWorktreeIsolationViolation ;
12- import org .testng .SkipException ;
1312import org .testng .annotations .Test ;
1413
1514import java .io .IOException ;
1615import java .nio .file .Files ;
1716import java .nio .file .Path ;
17+ import java .util .Map ;
1818
1919import static io .github .cowwoc .requirements13 .java .DefaultJavaValidators .requireThat ;
2020
@@ -677,26 +677,28 @@ public void readOnlyCommandIsAllowed() throws IOException
677677 @ Test
678678 public void allowsRedirectWhenEnvVarExpandsToWorktreePath () throws IOException
679679 {
680- String home = System .getenv ("HOME" );
681- if (home == null || home .isBlank ())
682- throw new SkipException ("HOME environment variable is not set; skipping env-var expansion test" );
683- Path projectPath = Files .createTempDirectory (Path .of (home ), "bwiv-test-" );
684- try (TestClaudeHook scope = new TestClaudeHook (projectPath , projectPath , projectPath ))
680+ Path fakeHome = Files .createTempDirectory ("fake-home-" );
681+ try
685682 {
686- TestUtils .writeLockFile (scope , ISSUE_ID , SESSION_ID );
687- Path worktreeDir = TestUtils .createWorktreeDir (scope , ISSUE_ID );
688- String relativePath = Path .of (home ).relativize (worktreeDir .resolve ("file.txt" )).toString ();
689- String command = "echo foo > ${HOME}/" + relativePath ;
683+ Path projectPath = Files .createTempDirectory (fakeHome , "bwiv-test-" );
684+ try (TestClaudeHook scope = new TestClaudeHook (projectPath , projectPath , projectPath ))
685+ {
686+ TestUtils .writeLockFile (scope , ISSUE_ID , SESSION_ID );
687+ Path worktreeDir = TestUtils .createWorktreeDir (scope , ISSUE_ID );
688+ String relativePath = fakeHome .relativize (worktreeDir .resolve ("file.txt" )).toString ();
689+ String command = "echo foo > ${HOME}/" + relativePath ;
690+ Map <String , String > env = Map .of ("HOME" , fakeHome .toString ());
690691
691- BlockWorktreeIsolationViolation handler = new BlockWorktreeIsolationViolation (scope );
692- BashHandler .Result result = handler .check (
693- TestUtils .bashHook (command , projectPath .toString (), SESSION_ID , scope ));
692+ BlockWorktreeIsolationViolation handler = new BlockWorktreeIsolationViolation (scope , env );
693+ BashHandler .Result result = handler .check (
694+ TestUtils .bashHook (command , projectPath .toString (), SESSION_ID , scope ));
694695
695- requireThat (result .blocked (), "blocked" ).isFalse ();
696+ requireThat (result .blocked (), "blocked" ).isFalse ();
697+ }
696698 }
697699 finally
698700 {
699- TestUtils .deleteDirectoryRecursively (projectPath );
701+ TestUtils .deleteDirectoryRecursively (fakeHome );
700702 }
701703 }
702704
@@ -711,26 +713,28 @@ public void allowsRedirectWhenEnvVarExpandsToWorktreePath() throws IOException
711713 @ Test
712714 public void allowsRedirectWhenBareEnvVarExpandsToWorktreePath () throws IOException
713715 {
714- String home = System .getenv ("HOME" );
715- if (home == null || home .isBlank ())
716- throw new SkipException ("HOME environment variable is not set; skipping env-var expansion test" );
717- Path projectPath = Files .createTempDirectory (Path .of (home ), "bwiv-test-" );
718- try (TestClaudeHook scope = new TestClaudeHook (projectPath , projectPath , projectPath ))
716+ Path fakeHome = Files .createTempDirectory ("fake-home-" );
717+ try
719718 {
720- TestUtils .writeLockFile (scope , ISSUE_ID , SESSION_ID );
721- Path worktreeDir = TestUtils .createWorktreeDir (scope , ISSUE_ID );
722- String relativePath = Path .of (home ).relativize (worktreeDir .resolve ("file.txt" )).toString ();
723- String command = "echo foo > $HOME/" + relativePath ;
719+ Path projectPath = Files .createTempDirectory (fakeHome , "bwiv-test-" );
720+ try (TestClaudeHook scope = new TestClaudeHook (projectPath , projectPath , projectPath ))
721+ {
722+ TestUtils .writeLockFile (scope , ISSUE_ID , SESSION_ID );
723+ Path worktreeDir = TestUtils .createWorktreeDir (scope , ISSUE_ID );
724+ String relativePath = fakeHome .relativize (worktreeDir .resolve ("file.txt" )).toString ();
725+ String command = "echo foo > $HOME/" + relativePath ;
726+ Map <String , String > env = Map .of ("HOME" , fakeHome .toString ());
724727
725- BlockWorktreeIsolationViolation handler = new BlockWorktreeIsolationViolation (scope );
726- BashHandler .Result result = handler .check (
727- TestUtils .bashHook (command , projectPath .toString (), SESSION_ID , scope ));
728+ BlockWorktreeIsolationViolation handler = new BlockWorktreeIsolationViolation (scope , env );
729+ BashHandler .Result result = handler .check (
730+ TestUtils .bashHook (command , projectPath .toString (), SESSION_ID , scope ));
728731
729- requireThat (result .blocked (), "blocked" ).isFalse ();
732+ requireThat (result .blocked (), "blocked" ).isFalse ();
733+ }
730734 }
731735 finally
732736 {
733- TestUtils .deleteDirectoryRecursively (projectPath );
737+ TestUtils .deleteDirectoryRecursively (fakeHome );
734738 }
735739 }
736740
@@ -746,27 +750,29 @@ public void allowsRedirectWhenBareEnvVarExpandsToWorktreePath() throws IOExcepti
746750 @ Test
747751 public void blocksRedirectWhenEnvVarExpandsOutsideWorktree () throws IOException
748752 {
749- String home = System .getenv ("HOME" );
750- if (home == null || home .isBlank ())
751- throw new SkipException ("HOME environment variable is not set; skipping env-var expansion test" );
752- Path projectPath = Files .createTempDirectory (Path .of (home ), "bwiv-test-" );
753- try (TestClaudeHook scope = new TestClaudeHook (projectPath , projectPath , projectPath ))
754- {
755- TestUtils .writeLockFile (scope , ISSUE_ID , SESSION_ID );
756- TestUtils .createWorktreeDir (scope , ISSUE_ID );
757- String relativePath = Path .of (home ).relativize (projectPath .resolve ("plugin/file.txt" )).toString ();
758- String command = "echo foo > ${HOME}/" + relativePath ;
759-
760- BlockWorktreeIsolationViolation handler = new BlockWorktreeIsolationViolation (scope );
761- BashHandler .Result result = handler .check (
762- TestUtils .bashHook (command , projectPath .toString (), SESSION_ID , scope ));
763-
764- requireThat (result .blocked (), "blocked" ).isTrue ();
765- requireThat (result .reason (), "reason" ).contains ("isolation violation" );
753+ Path fakeHome = Files .createTempDirectory ("fake-home-" );
754+ try
755+ {
756+ Path projectPath = Files .createTempDirectory (fakeHome , "bwiv-test-" );
757+ try (TestClaudeHook scope = new TestClaudeHook (projectPath , projectPath , projectPath ))
758+ {
759+ TestUtils .writeLockFile (scope , ISSUE_ID , SESSION_ID );
760+ TestUtils .createWorktreeDir (scope , ISSUE_ID );
761+ String relativePath = fakeHome .relativize (projectPath .resolve ("plugin/file.txt" )).toString ();
762+ String command = "echo foo > ${HOME}/" + relativePath ;
763+ Map <String , String > env = Map .of ("HOME" , fakeHome .toString ());
764+
765+ BlockWorktreeIsolationViolation handler = new BlockWorktreeIsolationViolation (scope , env );
766+ BashHandler .Result result = handler .check (
767+ TestUtils .bashHook (command , projectPath .toString (), SESSION_ID , scope ));
768+
769+ requireThat (result .blocked (), "blocked" ).isTrue ();
770+ requireThat (result .reason (), "reason" ).contains ("isolation violation" );
771+ }
766772 }
767773 finally
768774 {
769- TestUtils .deleteDirectoryRecursively (projectPath );
775+ TestUtils .deleteDirectoryRecursively (fakeHome );
770776 }
771777 }
772778
0 commit comments