From ac9e0bfb6d1c4f78bb5b142c2c99888adff2d538 Mon Sep 17 00:00:00 2001 From: Abdulsametileri Date: Mon, 18 Apr 2022 14:21:45 +0300 Subject: [PATCH] Refactor: Change implementation of testing Go packages from project root --- cmd/vx/cli/add_test.go | 1 + cmd/vx/cli/checkout_test.go | 2 ++ cmd/vx/cli/history_test.go | 1 + cmd/vx/cli/root.go | 9 --------- cmd/vx/cli/status_test.go | 1 + cmd/vx/cli/utils_test.go | 1 + testing/testing.go | 19 +++++++++++++++++++ 7 files changed, 25 insertions(+), 9 deletions(-) create mode 100644 testing/testing.go diff --git a/cmd/vx/cli/add_test.go b/cmd/vx/cli/add_test.go index 07663ac..3f7618b 100644 --- a/cmd/vx/cli/add_test.go +++ b/cmd/vx/cli/add_test.go @@ -3,6 +3,7 @@ package cli import ( "testing" + _ "github.com/Abdulsametileri/vX/testing" "github.com/stretchr/testify/assert" ) diff --git a/cmd/vx/cli/checkout_test.go b/cmd/vx/cli/checkout_test.go index bfd703d..07326b4 100644 --- a/cmd/vx/cli/checkout_test.go +++ b/cmd/vx/cli/checkout_test.go @@ -3,6 +3,8 @@ package cli import ( "testing" + _ "github.com/Abdulsametileri/vX/testing" + "github.com/stretchr/testify/assert" ) diff --git a/cmd/vx/cli/history_test.go b/cmd/vx/cli/history_test.go index 2f0be71..456f318 100644 --- a/cmd/vx/cli/history_test.go +++ b/cmd/vx/cli/history_test.go @@ -5,6 +5,7 @@ import ( "fmt" "testing" + _ "github.com/Abdulsametileri/vX/testing" "github.com/acarl005/stripansi" "github.com/stretchr/testify/assert" ) diff --git a/cmd/vx/cli/root.go b/cmd/vx/cli/root.go index d43e62d..deadd42 100644 --- a/cmd/vx/cli/root.go +++ b/cmd/vx/cli/root.go @@ -2,14 +2,12 @@ package cli import ( "log" - "os" "time" "github.com/spf13/cobra" ) func init() { - changeWorkingDirectoryToRootDir() changeGlobalTimeToUTC() } @@ -30,13 +28,6 @@ func Execute() { } } -func changeWorkingDirectoryToRootDir() { - err := os.Chdir("../../..") - if err != nil { - panic(err) - } -} - func changeGlobalTimeToUTC() { loc, err := time.LoadLocation("UTC") if err == nil { diff --git a/cmd/vx/cli/status_test.go b/cmd/vx/cli/status_test.go index 25796dc..66fed8d 100644 --- a/cmd/vx/cli/status_test.go +++ b/cmd/vx/cli/status_test.go @@ -4,6 +4,7 @@ import ( "bytes" "testing" + _ "github.com/Abdulsametileri/vX/testing" "github.com/acarl005/stripansi" "github.com/stretchr/testify/assert" ) diff --git a/cmd/vx/cli/utils_test.go b/cmd/vx/cli/utils_test.go index 30d3514..4df8d35 100644 --- a/cmd/vx/cli/utils_test.go +++ b/cmd/vx/cli/utils_test.go @@ -3,6 +3,7 @@ package cli import ( "testing" + _ "github.com/Abdulsametileri/vX/testing" "github.com/stretchr/testify/assert" ) diff --git a/testing/testing.go b/testing/testing.go new file mode 100644 index 0000000..8104767 --- /dev/null +++ b/testing/testing.go @@ -0,0 +1,19 @@ +package testing + +import ( + "os" + "path/filepath" + "runtime" +) + +func init() { + changeWorkingDirectoryToRootDir() +} + +func changeWorkingDirectoryToRootDir() { + _, filename, _, _ := runtime.Caller(0) + err := os.Chdir(filepath.Join(filepath.Dir(filename), "..")) + if err != nil { + panic(err) + } +}