-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stacked on top of #11 Co-authored-by: Brian McGee <[email protected]> Reviewed-on: https://git.numtide.com/numtide/treefmt/pulls/13 Co-authored-by: zimbatm <[email protected]> Co-committed-by: zimbatm <[email protected]>
- Loading branch information
1 parent
d8d666a
commit 96b1560
Showing
7 changed files
with
219 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package cli | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"git.numtide.com/numtide/treefmt/internal/test" | ||
"github.com/alecthomas/kong" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func newKong(t *testing.T, cli interface{}, options ...kong.Option) *kong.Kong { | ||
t.Helper() | ||
options = append([]kong.Option{ | ||
kong.Name("test"), | ||
kong.Exit(func(int) { | ||
t.Helper() | ||
t.Fatalf("unexpected exit()") | ||
}), | ||
}, options...) | ||
parser, err := kong.New(cli, options...) | ||
require.NoError(t, err) | ||
return parser | ||
} | ||
|
||
func cmd(t *testing.T, args ...string) ([]byte, error) { | ||
t.Helper() | ||
|
||
// create a new kong context | ||
p := newKong(t, &Cli) | ||
ctx, err := p.Parse(args) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
tempDir := t.TempDir() | ||
tempOut := test.TempFile(t, filepath.Join(tempDir, "combined_output")) | ||
|
||
// capture standard outputs before swapping them | ||
stdout := os.Stdout | ||
stderr := os.Stderr | ||
|
||
// swap them temporarily | ||
os.Stdout = tempOut | ||
os.Stderr = tempOut | ||
|
||
// run the command | ||
if err = ctx.Run(); err != nil { | ||
return nil, err | ||
} | ||
|
||
// reset and read the temporary output | ||
if _, err = tempOut.Seek(0, 0); err != nil { | ||
return nil, fmt.Errorf("%w: failed to reset temp output for reading", err) | ||
} | ||
|
||
out, err := io.ReadAll(tempOut) | ||
if err != nil { | ||
return nil, fmt.Errorf("%w: failed to read temp output", err) | ||
} | ||
|
||
// swap outputs back | ||
os.Stdout = stdout | ||
os.Stderr = stderr | ||
|
||
return out, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.