diff --git a/cmd/block_test.go b/cmd/block_test.go new file mode 100644 index 0000000..001509b --- /dev/null +++ b/cmd/block_test.go @@ -0,0 +1,7 @@ +package cmd_test + +import "testing" + +func TestBlockCommand(t *testing.T) { + runScript(t, "testdata/script/block.txtar") +} diff --git a/cmd/file.go b/cmd/file.go index 4cbddca..d0c78d4 100644 --- a/cmd/file.go +++ b/cmd/file.go @@ -2,22 +2,24 @@ package cmd import ( "fmt" - "io" "os" + "litdoc/internal" + "github.com/spf13/cobra" ) var fileCmd = &cobra.Command{ - Use: "file", + Use: "file ", Short: "Process a file", + Args: cobra.ExactArgs(1), Run: func(cmd *cobra.Command, args []string) { - data, err := io.ReadAll(os.Stdin) + data, err := internal.ProcessFile(args[0]) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } - fmt.Print(string(data)) + fmt.Print(data) }, } diff --git a/cmd/file_test.go b/cmd/file_test.go new file mode 100644 index 0000000..62200d7 --- /dev/null +++ b/cmd/file_test.go @@ -0,0 +1,34 @@ +package cmd_test + +import ( + "fmt" + "os" + "testing" + + "litdoc/cmd" + + "github.com/rogpeppe/go-internal/testscript" +) + +func TestMain(m *testing.M) { + testscript.Main(m, map[string]func(){ + "litdoc": func() { + if err := cmd.Execute(); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + }, + }) +} + +func TestFileCommand(t *testing.T) { + runScript(t, "testdata/script/file.txtar") +} + +func runScript(t *testing.T, script string) { + t.Helper() + + testscript.Run(t, testscript.Params{ + Files: []string{script}, + }) +} diff --git a/testdata/script/block.txtar b/cmd/testdata/script/block.txtar similarity index 100% rename from testdata/script/block.txtar rename to cmd/testdata/script/block.txtar diff --git a/testdata/script/file.txtar b/cmd/testdata/script/file.txtar similarity index 56% rename from testdata/script/file.txtar rename to cmd/testdata/script/file.txtar index 5809cf5..d8aafae 100644 --- a/testdata/script/file.txtar +++ b/cmd/testdata/script/file.txtar @@ -1,5 +1,4 @@ -stdin input.md -exec litdoc file +exec litdoc file input.md stdout '# Hello' -- input.md -- diff --git a/main_test.go b/main_test.go deleted file mode 100644 index 36bbea9..0000000 --- a/main_test.go +++ /dev/null @@ -1,23 +0,0 @@ -package main_test - -import ( - "os" - "path/filepath" - "testing" - - "github.com/rogpeppe/go-internal/testscript" -) - -func TestScript(t *testing.T) { - binPath, err := filepath.Abs("bin") - if err != nil { - t.Fatal(err) - } - testscript.Run(t, testscript.Params{ - Dir: "testdata/script", - Setup: func(env *testscript.Env) error { - env.Setenv("PATH", binPath+string(os.PathListSeparator)+env.Getenv("PATH")) - return nil - }, - }) -}