From 9d6e735027479c7085a245651bc6f21d8aa38ad9 Mon Sep 17 00:00:00 2001 From: KitsuneSemCalda Date: Fri, 24 May 2024 00:57:50 -0300 Subject: [PATCH] :sparkles: feature: implement a custom argparse with tests --- .github/workflows/go.yml | 10 +++++----- cmd/animatic-v2/main.go | 5 +++++ internal/argparse/parse.go | 19 +++++++++++++++++++ internal/argparse/types.go | 5 +++++ test/argparse/parse_test.go | 35 +++++++++++++++++++++++++++++++++++ 5 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 cmd/animatic-v2/main.go create mode 100644 internal/argparse/parse.go create mode 100644 internal/argparse/types.go create mode 100644 test/argparse/parse_test.go diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 41f7a4d..cbdfa38 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -2,9 +2,9 @@ name: Go Testing on: push: - branches: ["master", "*"] + branches: ["third_version", "*"] pull_request: - branches: ["master", "*"] + branches: ["third_version", "*"] jobs: build: @@ -18,10 +18,10 @@ jobs: id: go - name: Check out code into the Go module directory - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Get dependencies run: go get -v -t -d ./... - #- name: Test - # run: go test -v ./... + - name: Test + run: go test -v ./... diff --git a/cmd/animatic-v2/main.go b/cmd/animatic-v2/main.go new file mode 100644 index 0000000..9e1efcd --- /dev/null +++ b/cmd/animatic-v2/main.go @@ -0,0 +1,5 @@ +package main + +func main(){ + println("Hello World!") +} diff --git a/internal/argparse/parse.go b/internal/argparse/parse.go new file mode 100644 index 0000000..c43073c --- /dev/null +++ b/internal/argparse/parse.go @@ -0,0 +1,19 @@ +package argparse + +import ( + "flag" + "os" +) + +/* +Parse the user input given a `flag.FlagSet` to be easy testing +*/ +func ParseArgs(fs *flag.FlagSet) Args{ + arguments := Args{} + + fs.BoolVar(&arguments.DebugMode, "debug" , false, "this function enable debug") + + fs.Parse(os.Args[1:]) + + return arguments +} diff --git a/internal/argparse/types.go b/internal/argparse/types.go new file mode 100644 index 0000000..10b1338 --- /dev/null +++ b/internal/argparse/types.go @@ -0,0 +1,5 @@ +package argparse + +type Args struct { + DebugMode bool +} diff --git a/test/argparse/parse_test.go b/test/argparse/parse_test.go new file mode 100644 index 0000000..a0cd98d --- /dev/null +++ b/test/argparse/parse_test.go @@ -0,0 +1,35 @@ +package argparse_test + +import ( + "Animatic-v2/internal/argparse" + "flag" + "os" + "testing" + +) + +func TestArgparse(t *testing.T){ + t.Run("Should return the struct if debug setted to false", func(t *testing.T) { + fs := flag.NewFlagSet("test", flag.ContinueOnError) + + os.Args = []string{"cmd", "-debug=false"} + + argv := argparse.ParseArgs(fs) + + if argv.DebugMode != false { + t.Fatalf("expected %v instead %v\n", !argv.DebugMode, argv.DebugMode) + } + }) + + t.Run("Should return the struct if debug setted to true", func(t *testing.T) { + fs := flag.NewFlagSet("test", flag.ContinueOnError) + + os.Args = []string{"cmd", "-debug"} + + argv := argparse.ParseArgs(fs) + + if argv.DebugMode != true { + t.Fatalf("expected %v instead %v\n", argv.DebugMode, !argv.DebugMode) + } + }) +}