diff options
-rw-r--r-- | golings/cmd/cmd_suite_test.go | 73 | ||||
-rw-r--r-- | golings/cmd/list_suite_test.go | 27 | ||||
-rw-r--r-- | golings/cmd/root.go | 12 | ||||
-rw-r--r-- | golings/cmd/run.go | 56 | ||||
-rw-r--r-- | golings/fixtures/error1/info.toml | 5 | ||||
-rw-r--r-- | golings/fixtures/error1/main.go | 10 | ||||
-rw-r--r-- | golings/fixtures/pending1/info.toml | 5 | ||||
-rw-r--r-- | golings/fixtures/pending1/main.go | 9 | ||||
-rw-r--r-- | golings/fixtures/success1/info.toml | 5 | ||||
-rw-r--r-- | golings/fixtures/success1/main.go | 7 |
10 files changed, 151 insertions, 58 deletions
diff --git a/golings/cmd/cmd_suite_test.go b/golings/cmd/cmd_suite_test.go new file mode 100644 index 0000000..92f77b1 --- /dev/null +++ b/golings/cmd/cmd_suite_test.go @@ -0,0 +1,73 @@ +package cmd_test + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + + "github.com/mauricioabreu/golings/golings/cmd" +) + +func TestCmd(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Commands Suite") +} + +var _ = Describe("Commands", func() { + Describe("List", func() { + Context("List exercises", func() { + It("returns a list of exercises", func() { + list := cmd.ListCmd("../fixtures/info.toml") + + err := list.Execute() + + Expect(err).ToNot(HaveOccurred()) + }) + }) + }) + Describe("Run", func() { + Context("Running 'compile' mode exercises", func() { + When("it is compilable", func() { + It("returns success", func() { + run := cmd.RunCmd("../fixtures/success1/info.toml") + run.SetArgs([]string{"success1"}) + + err := run.Execute() + + Expect(err).ToNot(HaveOccurred()) + }) + }) + When("it is compilable but it is pending", func() { + It("returns error", func() { + run := cmd.RunCmd("../fixtures/pending1/info.toml") + run.SetArgs([]string{"pending1"}) + + err := run.Execute() + + Expect(err).To(HaveOccurred()) + }) + }) + When("it is not compilable", func() { + It("returns error", func() { + run := cmd.RunCmd("../fixtures/error1/info.toml") + run.SetArgs([]string{"error1"}) + + err := run.Execute() + + Expect(err).To(HaveOccurred()) + }) + }) + When("it does not exist", func() { + It("returns an error", func() { + run := cmd.RunCmd("../fixtures/info.toml") + run.SetArgs([]string{"404"}) + + err := run.Execute() + + Expect(err).To(HaveOccurred()) + }) + }) + }) + }) +}) diff --git a/golings/cmd/list_suite_test.go b/golings/cmd/list_suite_test.go deleted file mode 100644 index c7e79e6..0000000 --- a/golings/cmd/list_suite_test.go +++ /dev/null @@ -1,27 +0,0 @@ -package cmd_test - -import ( - "testing" - - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - - "github.com/mauricioabreu/golings/golings/cmd" -) - -func TestList(t *testing.T) { - RegisterFailHandler(Fail) - RunSpecs(t, "List Suite") -} - -var _ = Describe("List", func() { - Context("List exercises", func() { - It("returns a list of exercises", func() { - list := cmd.ListCmd("../fixtures/info.toml") - - err := list.Execute() - - Expect(err).ToNot(HaveOccurred()) - }) - }) -}) diff --git a/golings/cmd/root.go b/golings/cmd/root.go index 4d0880a..c0e08af 100644 --- a/golings/cmd/root.go +++ b/golings/cmd/root.go @@ -1,7 +1,6 @@ package cmd import ( - "fmt" "os" "github.com/spf13/cobra" @@ -9,14 +8,16 @@ import ( func NewRootCmd(version string) *cobra.Command { rootCmd := &cobra.Command{ - Use: "golings", - Short: "Learn go through interactive exercises", - Version: version, + Use: "golings", + Short: "Learn go through interactive exercises", + SilenceUsage: true, + SilenceErrors: true, + Version: version, } rootCmd.AddCommand(cmdHint) rootCmd.AddCommand(ListCmd("info.toml")) - rootCmd.AddCommand(cmdRun) + rootCmd.AddCommand(RunCmd("info.toml")) rootCmd.AddCommand(cmdVerify) return rootCmd @@ -24,7 +25,6 @@ func NewRootCmd(version string) *cobra.Command { func Execute(version string) { if err := NewRootCmd(version).Execute(); err != nil { - fmt.Println(err) os.Exit(1) } } diff --git a/golings/cmd/run.go b/golings/cmd/run.go index ff3fa70..bf909a4 100644 --- a/golings/cmd/run.go +++ b/golings/cmd/run.go @@ -2,35 +2,41 @@ package cmd import ( "errors" - "os" + "fmt" "github.com/fatih/color" "github.com/mauricioabreu/golings/golings/exercises" "github.com/spf13/cobra" ) -var cmdRun = &cobra.Command{ - Use: "run [exercise]", - Short: "Run a single exercise", - Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), - Run: func(cmd *cobra.Command, args []string) { - result, err := exercises.Run(args[0], "info.toml") - if errors.Is(err, exercises.ErrExerciseNotFound) { - color.White("No exercise found for '%s'", args[0]) - os.Exit(1) - } else if err != nil { - color.Cyan("Failed to compile the exercise %s\n\n", result.Exercise.Path) - color.White("Check the output below: \n\n") - color.Red(result.Err) - color.Red(result.Out) - color.Yellow("If you feel stuck, ask a hint by executing `golings hint %s`", result.Exercise.Name) - os.Exit(1) - } else { - color.Green("Congratulations!\n\n") - color.Green("Remove the 'I AM NOT DONE' from the file to keep going\n") - color.Green("Here is the output of your program:\n\n") - color.Cyan(result.Out) - os.Exit(0) - } - }, +func RunCmd(infoFile string) *cobra.Command { + return &cobra.Command{ + Use: "run [exercise]", + Short: "Run a single exercise", + Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs), + SilenceUsage: true, + SilenceErrors: true, + RunE: func(cmd *cobra.Command, args []string) error { + result, err := exercises.Run(args[0], infoFile) + if errors.Is(err, exercises.ErrExerciseNotFound) { + color.White("No exercise found for '%s'", args[0]) + } else if err != nil { + color.Cyan("Failed to compile the exercise %s\n\n", result.Exercise.Path) + color.White("Check the output below: \n\n") + color.Red(result.Err) + color.Red(result.Out) + color.Yellow("If you feel stuck, ask a hint by executing `golings hint %s`", result.Exercise.Name) + } else { + color.Green("Congratulations!\n\n") + color.Green("Here is the output of your program:\n\n") + color.Cyan(result.Out) + if result.Exercise.State() == exercises.Pending { + color.White("Remove the 'I AM NOT DONE' from the file to keep going\n") + return fmt.Errorf("exercise is still pending") + } + } + + return err + }, + } } diff --git a/golings/fixtures/error1/info.toml b/golings/fixtures/error1/info.toml new file mode 100644 index 0000000..1387353 --- /dev/null +++ b/golings/fixtures/error1/info.toml @@ -0,0 +1,5 @@ +[[exercises]] +name = "error1" +path = "../fixtures/error1/main.go" +mode = "compile" +hint = "" diff --git a/golings/fixtures/error1/main.go b/golings/fixtures/error1/main.go new file mode 100644 index 0000000..1f54101 --- /dev/null +++ b/golings/fixtures/error1/main.go @@ -0,0 +1,10 @@ +//go:build !test +// +build !test + +// success1 +// Make me compile! +package main + +func main() { + a := 10 +} diff --git a/golings/fixtures/pending1/info.toml b/golings/fixtures/pending1/info.toml new file mode 100644 index 0000000..94ad86b --- /dev/null +++ b/golings/fixtures/pending1/info.toml @@ -0,0 +1,5 @@ +[[exercises]] +name = "pending1" +path = "../fixtures/pending1/main.go" +mode = "compile" +hint = "" diff --git a/golings/fixtures/pending1/main.go b/golings/fixtures/pending1/main.go new file mode 100644 index 0000000..fd8c2b3 --- /dev/null +++ b/golings/fixtures/pending1/main.go @@ -0,0 +1,9 @@ +// success1 +// Make me compile! + +// I AM NOT DONE +package main + +func main() { + +} diff --git a/golings/fixtures/success1/info.toml b/golings/fixtures/success1/info.toml new file mode 100644 index 0000000..65d8c4b --- /dev/null +++ b/golings/fixtures/success1/info.toml @@ -0,0 +1,5 @@ +[[exercises]] +name = "success1" +path = "../fixtures/success1/main.go" +mode = "compile" +hint = "" diff --git a/golings/fixtures/success1/main.go b/golings/fixtures/success1/main.go new file mode 100644 index 0000000..f27d62a --- /dev/null +++ b/golings/fixtures/success1/main.go @@ -0,0 +1,7 @@ +// success1 +// Make me compile! +package main + +func main() { + +} |