Skip to content

Commit

Permalink
feat: inputs/outputs in flow and test harness (#14)
Browse files Browse the repository at this point in the history
* lots of moving stuff around cleanup/refactor

* functioning commit - input/output processing looks to work

* added functioning regex match input/output

* test cases proving positional

* refactor: moved things to interface impl for testing

* test: updated test harness with ability to start testing

* chore: moved mocks around

* fix: corrected lint
  • Loading branch information
madhuravius authored Jul 20, 2022
1 parent b2856ef commit 55e9890
Show file tree
Hide file tree
Showing 31 changed files with 899 additions and 201 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ jobs:
- uses: actions/setup-go@v3
with:
go-version: '>=1.18.0'
- run: go test ./... -cover
- run: go test ./... -v -cover
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
init:
go mod download
go generate ./...
.PHONY: init

build:
Expand All @@ -14,7 +15,9 @@ start:
.PHONY: start

clean:
rm build/clingy
rm build/clingy || true
go run *.go clean
go mod tidy
.PHONY: clean

lint:
Expand All @@ -27,7 +30,7 @@ lint:
.PHONY: lint

test:
go test ./... -cover
go test ./... -v -cover
.PHONY: test

pretty:
Expand Down
46 changes: 24 additions & 22 deletions cmd/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,31 @@ import (
"github.com/spf13/cobra"
)

// cleanCmd - clean temporary paths
var cleanCmd = &cobra.Command{
Use: "clean",
Short: "Clean clingy",
PreRun: initRunWithoutArtifactDirectoryCreate,
Run: func(cmd *cobra.Command, args []string) {
logger.Println("Cleaning clingy generated files")
// newCleanCmd - clean temporary paths
func (r *RootConfig) newCleanCmd() *cobra.Command {
return &cobra.Command{
Use: "clean",
Short: "Clean clingy",
PreRun: initRunWithoutArtifactDirectoryCreate,
Run: func(cmd *cobra.Command, args []string) {
logger.Println("Cleaning clingy generated files")

subPaths, err := ioutil.ReadDir(outputPath)
if err != nil {
logger.Println("Unable to read build directory for cleaning", err)
os.Exit(1)
}

for _, subPath := range subPaths {
if subPath.Name() == ".gitkeep" {
continue
}
err := os.RemoveAll(path.Join([]string{outputPath, subPath.Name()}...))
subPaths, err := ioutil.ReadDir(outputPath)
if err != nil {
logger.Println("Unable to clean up normal build path", err)
os.Exit(1)
logger.Println("Unable to read build directory for cleaning", err)
r.ExitTools.Exit(1)
}

for _, subPath := range subPaths {
if subPath.Name() == ".gitkeep" {
continue
}
err := os.RemoveAll(path.Join([]string{outputPath, subPath.Name()}...))
if err != nil {
logger.Println("Unable to clean up normal build path", err)
r.ExitTools.Exit(1)
}
}
}
},
},
}
}
55 changes: 29 additions & 26 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,34 @@ steps:
args:
- "Starting"`

// initCmd - inits a .clingy.yaml for use in the current path
var initCmd = &cobra.Command{
Use: "init",
Short: "instantiate a .clingy.yaml for use in the cwd",
PreRun: initRunWithoutArtifactDirectoryCreate,
Run: func(cmd *cobra.Command, args []string) {
// check current path to determine if needing to write file
logger.Println("Checking if inputFile exists already", inputFile)
fileInfo, err := os.Stat(inputFile)
if err != nil && !strings.Contains(err.Error(), "no such file or directory") {
logger.Println("Error in os stat for file info", err)
fmt.Println("Error in looking up path to write the file")
os.Exit(1)
}
if fileInfo != nil {
logger.Println("File found: ", fileInfo.Name())
fmt.Println("File already exists!")
os.Exit(1)
}
// newInitCmd - inits a .clingy.yaml for use in the current path
func (r *RootConfig) newInitCmd() *cobra.Command {
return &cobra.Command{
Use: "init",
Short: "instantiate a .clingy.yaml for use in the cwd",
PreRun: initRunWithoutArtifactDirectoryCreate,
Run: func(cmd *cobra.Command, args []string) {
// check current path to determine if needing to write file
logger.Println("Checking if inputFile exists already", inputFile)
fileInfo, err := os.Stat(inputFile)
if err != nil && !strings.Contains(err.Error(), "no such file or directory") {
logger.Println("Error in os stat for file info", err)
fmt.Println("Error in looking up path to write the file")
r.ExitTools.Exit(1)
}
if fileInfo != nil {
logger.Println("File found: ", fileInfo.Name())
fmt.Println("File already exists!")
r.ExitTools.Exit(1)
}

// if it doesn't exist, go ahead and write the default template
if err := os.WriteFile(inputFile, []byte(templateFile), 0644); err != nil {
logger.Println("Error in writing file", err)
fmt.Println("Error in trying to write template file")
r.ExitTools.Exit(1)
}
},
}

// if it doesn't exist, go ahead and write the default template
if err := os.WriteFile(inputFile, []byte(templateFile), 0644); err != nil {
logger.Println("Error in writing file", err)
fmt.Println("Error in trying to write template file")
os.Exit(1)
}
},
}
71 changes: 40 additions & 31 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"fmt"
"log"
"os"
"strconv"
"time"

Expand All @@ -17,7 +16,7 @@ var (
// logger - logger for debugging reasons, init'ed and typically writes to file in output directory w/ build #
logger *log.Logger
// version - version of the app to spit out, currently manually set :(
version = "v0.1.0"
version = "v0.3.0"

// flags
// debug - enable verbose logging
Expand All @@ -30,6 +29,12 @@ var (
reportStyle = "html-simple"
)

// RootConfig - variables to pass in for reuse and testing
type RootConfig struct {
ExitTools internal.ExitToolsImpl
Magick internal.MagickClientImpl
}

// getOutputPath - a string that generates a union of an (dynamic) output path and build number for artifacts
func getOutputPath() string {
return fmt.Sprintf("%s/%s", outputPath, buildNumber)
Expand All @@ -49,42 +54,46 @@ func initRunWithoutArtifactDirectoryCreate(_ *cobra.Command, _ []string) {
logger = internal.InitLogger(getOutputPath(), debug)
}

// rootCmd - entrypoint for clingy app
var rootCmd = &cobra.Command{
Use: "clingy",
Short: "clingy is a tool to test and capture CLI flows",
Long: `clingy is a tool to test and capture CLI flows.`,
PreRun: initRunWithoutArtifactDirectoryCreate,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
if err := cmd.Help(); err != nil {
logger.Println("Error when trying to print help.", err)
os.Exit(1)
// RootCmd - entrypoint for clingy app
func RootCmd(c *RootConfig) *cobra.Command {
rootCmd := &cobra.Command{
Use: "clingy",
Short: "clingy is a tool to test and capture CLI flows",
Long: `clingy is a tool to test and capture CLI flows.`,
PreRun: initRunWithoutArtifactDirectoryCreate,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
if err := cmd.Help(); err != nil {
logger.Println("Error when trying to print help.", err)
panic(err)
}
}
os.Exit(0)
}
},
}

// Execute ...
func Execute() {
if err := rootCmd.Execute(); err != nil {
logger.Println("Error when trying to execute", err)
os.Exit(1)
},
}
}

// init ...
func init() {
rootCmd.AddCommand(cleanCmd)
rootCmd.AddCommand(initCmd)
rootCmd.AddCommand(runCmd)
rootCmd.AddCommand(validateCmd)
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(c.newCleanCmd())
rootCmd.AddCommand(c.newInitCmd())
rootCmd.AddCommand(c.newRunCmd())
rootCmd.AddCommand(c.newValidateCmd())
rootCmd.AddCommand(c.newVersionCmd())

rootCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "enable debug logs")
rootCmd.PersistentFlags().StringVarP(&outputPath, "outputPath", "o", outputPath, "build path that dumps outputs")
rootCmd.PersistentFlags().StringVarP(&inputFile, "inputFile", "i", inputFile, "input file representing a .clingy.yaml")
rootCmd.PersistentFlags().StringVarP(&reportStyle, "reportStyle", "r", reportStyle, "report style to output to (choices: 'html-simple', 'images-only')")
rootCmd.Flags().SortFlags = true

return rootCmd
}

// Execute ...
func Execute() {
rootConfig := &RootConfig{
ExitTools: internal.NewExitToolsClient(),
Magick: internal.NewMagickClient(),
}
if err := RootCmd(rootConfig).Execute(); err != nil {
logger.Println("Error when trying to execute", err)
rootConfig.ExitTools.Exit(1)
}
}
27 changes: 27 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cmd

import (
"testing"

"github.com/stretchr/testify/assert"

"clingy/internal"
)

func TestRootCmdExecuteInstantiation(t *testing.T) {
mockTools := internal.GenerateMockInterfacesForClingy(t)
defer mockTools.Ctrl.Finish()

cmd := RootCmd(&RootConfig{Magick: mockTools.MagickClientImpl})
output := internal.ExecCobraCmdAndReturnString(t, cmd, []string{})
assert.Contains(t, output, "clingy is a tool to test and capture CLI flows")
}

func TestRootCmdExecuteHelp(t *testing.T) {
mockTools := internal.GenerateMockInterfacesForClingy(t)
defer mockTools.Ctrl.Finish()

cmd := RootCmd(&RootConfig{Magick: mockTools.MagickClientImpl})
output := internal.ExecCobraCmdAndReturnString(t, cmd, []string{"--help"})
assert.Contains(t, output, "clingy is a tool to test and capture CLI flows")
}
Loading

0 comments on commit 55e9890

Please sign in to comment.