@@ -29,8 +29,8 @@ import (
2929)
3030
3131const (
32- stdoutGoldenFile = "stdout.txt"
33- stderrGoldenFile = "stderr.txt"
32+ stdoutGoldenFileName = "stdout.txt"
33+ stderrGoldenFileName = "stderr.txt"
3434)
3535
3636type TestCase struct {
@@ -39,6 +39,8 @@ type TestCase struct {
3939 IsError bool
4040 Update bool
4141 ShowStdout bool
42+
43+ tempDir string
4244}
4345
4446func (tc TestCase ) Run (t * testing.T ) {
@@ -48,16 +50,16 @@ func (tc TestCase) Run(t *testing.T) {
4850
4951func (tc TestCase ) run (t * testing.T ) {
5052 // Replicate the "before" directory in the test temp directory.
51- tempDir : = t .TempDir ()
52- paths , err := tempfile .ReplicateDirectory (tc .testFolderBeforePath (), tempDir )
53+ tc . tempDir = t .TempDir ()
54+ paths , err := tempfile .ReplicateDirectory (tc .testFolderBeforePath (), tc . tempDir )
5355 assert .NilErr (t , err )
5456 err = paths .CreateAll ()
5557 assert .NilErr (t , err )
5658
5759 // Run the command for the test in the temp directory.
5860 var stdoutBuf bytes.Buffer
5961 var stderrBuf bytes.Buffer
60- cmd := tc .command (tempDir , & stdoutBuf , & stderrBuf )
62+ cmd := tc .command (& stdoutBuf , & stderrBuf )
6163 err = cmd .Run ()
6264 if tc .IsError {
6365 assert .NotNilErr (t , err )
@@ -71,15 +73,15 @@ func (tc TestCase) run(t *testing.T) {
7173 assert .NilErr (t , err )
7274 err = tc .goldenStderr (stderrBuf .Bytes ())
7375 assert .NilErr (t , err )
74- err = tc .goldenAfter (tempDir )
76+ err = tc .goldenAfter (tc . tempDir )
7577 assert .NilErr (t , err )
7678}
7779
7880func (tc TestCase ) testFolderBeforePath () string {
7981 return tc .testdataDirPath () + "/before"
8082}
8183
82- func (tc TestCase ) command (wd string , stdoutBuf * bytes.Buffer , stderrBuf * bytes.Buffer ) * exec.Cmd {
84+ func (tc TestCase ) command (stdoutBuf * bytes.Buffer , stderrBuf * bytes.Buffer ) * exec.Cmd {
8385 cmdArgs := []string {}
8486 for _ , arg := range strings .Split (tc .Command , " " ) {
8587 // This is to handle potential typos in args with extra spaces.
@@ -92,7 +94,7 @@ func (tc TestCase) command(wd string, stdoutBuf *bytes.Buffer, stderrBuf *bytes.
9294 Args : cmdArgs , // Args needs to be an array of everything including the command
9395 Stdout : stdoutBuf ,
9496 Stderr : stderrBuf ,
95- Dir : wd ,
97+ Dir : tc . tempDir ,
9698 }
9799}
98100
@@ -102,10 +104,14 @@ func (tc TestCase) goldenStdout(stdoutResult []byte) error {
102104 return nil
103105 }
104106 goldenCtx := tempfile.GoldenCtx {
105- Dir : tc .testFolderStdoutPath (),
106- Update : tc .Update ,
107+ GoldenDir : tc .testFolderStdoutPath (),
108+ ResultDir : tc .tempDir ,
109+ Update : tc .Update ,
107110 }
108- return goldenCtx .CompareGoldenFile (stdoutGoldenFile , stdoutResult )
111+ return goldenCtx .CompareGoldenFile (
112+ filepath .Join (tc .tempDir , stdoutGoldenFileName ),
113+ stdoutResult ,
114+ )
109115}
110116
111117func (tc TestCase ) goldenStderr (stderrResult []byte ) error {
@@ -114,16 +120,21 @@ func (tc TestCase) goldenStderr(stderrResult []byte) error {
114120 return nil
115121 }
116122 goldenCtx := tempfile.GoldenCtx {
117- Dir : tc .testFolderStdoutPath (),
118- Update : tc .Update ,
123+ GoldenDir : tc .testFolderStdoutPath (),
124+ ResultDir : tc .tempDir ,
125+ Update : tc .Update ,
119126 }
120- return goldenCtx .CompareGoldenFile (stderrGoldenFile , stderrResult )
127+ return goldenCtx .CompareGoldenFile (
128+ filepath .Join (tc .tempDir , stderrGoldenFileName ),
129+ stderrResult ,
130+ )
121131}
122132
123133func (tc TestCase ) goldenAfter (wd string ) error {
124134 goldenCtx := tempfile.GoldenCtx {
125- Dir : tc .testFolderAfterPath (),
126- Update : tc .Update ,
135+ GoldenDir : tc .testFolderAfterPath (),
136+ ResultDir : tc .tempDir ,
137+ Update : tc .Update ,
127138 }
128139 return goldenCtx .CompareDirectory (wd )
129140}
0 commit comments