From 7415e92ffc377fc0c704cd4e6e66eb4ce061c404 Mon Sep 17 00:00:00 2001 From: johnabass Date: Fri, 15 Oct 2021 10:23:46 -0700 Subject: [PATCH] verify that the main application container works properly --- CHANGELOG.md | 1 + main.go | 14 ++++++------- main_test.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 main_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 1cefc60..ef81d80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] - Refactored Switch into a simpler interface that removes internal concurrency - Ensure that as much as possible test logging goes through testing.T +- Added tests for the main DI container ## [v0.1.0] - README text diff --git a/main.go b/main.go index d87c18a..6d494dc 100644 --- a/main.go +++ b/main.go @@ -7,8 +7,8 @@ import ( "go.uber.org/fx" ) -func run(args []string) error { - app := fx.New( +func newApp(args []string) *fx.App { + return fx.New( parseCommandLine(args), provideLogger(os.Stdout), provideActions(), @@ -16,15 +16,13 @@ func run(args []string) error { provideSwitch(), provideHTTP(), ) - - app.Run() - return app.Err() } func main() { - err := run(os.Args[1:]) - if err != nil { - fmt.Fprintf(os.Stderr, "%s\n", err) + app := newApp(os.Args[1:]) + app.Run() + if app.Err() != nil { + fmt.Fprintf(os.Stderr, "%s\n", app.Err()) os.Exit(1) } } diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..a3765ac --- /dev/null +++ b/main_test.go @@ -0,0 +1,55 @@ +package main + +import ( + "strconv" + "testing" + + "github.com/stretchr/testify/suite" +) + +type NewAppSuite struct { + DMSSuite + + validParameters [][]string + invalidParameters [][]string +} + +func (suite *NewAppSuite) SetupSuite() { + suite.validParameters = [][]string{ + {"--exec", "echo 'hi'"}, + {"--exec", "echo 'hi'", "--ttl", "10s", "--misses", "4"}, + {"--exec", "echo 'hi'", "--exec", "echo 'another'", "--ttl", "12h", "--misses", "2", "--debug"}, + } + + suite.invalidParameters = [][]string{ + {}, + {"--foobar"}, + {"--exec", "echo 'hi'", "--foobar"}, + } +} + +func (suite *NewAppSuite) TestNewApp() { + suite.Run("Valid", func() { + for i, validParameters := range suite.validParameters { + suite.Run(strconv.Itoa(i), func() { + app := newApp(validParameters) + suite.Require().NotNil(app) + suite.NoError(app.Err()) + }) + } + }) + + suite.Run("Invalid", func() { + for i, invalidParameters := range suite.invalidParameters { + suite.Run(strconv.Itoa(i), func() { + app := newApp(invalidParameters) + suite.Require().NotNil(app) + suite.Error(app.Err()) + }) + } + }) +} + +func TestNewApp(t *testing.T) { + suite.Run(t, new(NewAppSuite)) +}