From 6aacb81624f395031c7998b3c460db3a71e3828d Mon Sep 17 00:00:00 2001 From: better0fdead Date: Mon, 5 Dec 2022 16:43:43 +0300 Subject: [PATCH] tt: fix unit tests Fixed unit tests. Since /private/var and /var/ in macOS are symlinks, there is no real difference between them and it should not cause error. Added macOS support for TestGetVersions. Follow up #254 Closes #170 --- cli/build/build_test.go | 28 +++++++++++++++++++++---- cli/configure/configure_test.go | 10 ++++++++- cli/install_ee/install_ee_test.go | 35 +++++++++++++++++++++++++++---- cli/util/util.go | 10 +++++++-- 4 files changed, 72 insertions(+), 11 deletions(-) diff --git a/cli/build/build_test.go b/cli/build/build_test.go index 50918a8e3..43f1cc3e1 100644 --- a/cli/build/build_test.go +++ b/cli/build/build_test.go @@ -9,6 +9,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/tarantool/tt/cli/util" ) const testDirName = "build-test-dir" @@ -24,17 +25,26 @@ func TestFillCtx(t *testing.T) { require.NoError(t, os.Chdir(workDir)) defer os.Chdir(wd) var buildCtx BuildCtx + + osType, _ := util.GetOs() + switch osType { + case util.OsMacos: + workDir = filepath.Join("/private", workDir) + } + appDir := filepath.Join(workDir, "app1") + appDir2 := filepath.Join(workDir, "app2") + require.NoError(t, FillCtx(&buildCtx, []string{"app1"})) - assert.Equal(t, buildCtx.BuildDir, filepath.Join(workDir, "app1")) + assert.Equal(t, buildCtx.BuildDir, appDir) require.NoError(t, FillCtx(&buildCtx, []string{"./app1"})) - assert.Equal(t, buildCtx.BuildDir, filepath.Join(workDir, "app1")) + assert.Equal(t, buildCtx.BuildDir, appDir) require.NoError(t, FillCtx(&buildCtx, []string{})) assert.Equal(t, buildCtx.BuildDir, workDir) require.EqualError(t, FillCtx(&buildCtx, []string{"app1", "app2"}), "too many args") require.EqualError(t, FillCtx(&buildCtx, []string{"app2"}), - fmt.Sprintf("stat %s: no such file or directory", filepath.Join(workDir, "app2"))) + fmt.Sprintf("stat %s: no such file or directory", appDir2)) require.NoError(t, FillCtx(&buildCtx, []string{filepath.Join(workDir, "app1")})) assert.Equal(t, buildCtx.BuildDir, filepath.Join(workDir, "app1")) @@ -62,8 +72,18 @@ func TestFillCtxAppPathIsFile(t *testing.T) { require.NoError(t, os.Chdir(workDir)) defer os.Chdir(wd) var buildCtx BuildCtx + + osType, _ := util.GetOs() + appDir := "" + switch osType { + case util.OsMacos: + appDir = filepath.Join("/private", workDir, "app1") + case util.OsLinux: + appDir = filepath.Join(workDir, "app1") + } + require.EqualError(t, FillCtx(&buildCtx, []string{"app1"}), - fmt.Sprintf("%s is not a directory", filepath.Join(workDir, "app1"))) + fmt.Sprintf("%s is not a directory", appDir)) } func TestFillCtxMultipleArgs(t *testing.T) { diff --git a/cli/configure/configure_test.go b/cli/configure/configure_test.go index 21cac56c8..ba8903742 100644 --- a/cli/configure/configure_test.go +++ b/cli/configure/configure_test.go @@ -216,6 +216,14 @@ func TestGetConfigPath(t *testing.T) { require.NoError(t, os.Remove(filepath.Join(tempDir, "a", "tarantool.yaml"))) configName, err = getConfigPath(ConfigName) - assert.Equal(t, filepath.Join(tempDir, "a", "tarantool.yml"), configName) + + osType, _ := util.GetOs() + prefix := "" + switch osType { + case util.OsMacos: + prefix = "/private" + } + + assert.Equal(t, filepath.Join(prefix, tempDir, "a", "tarantool.yml"), configName) assert.NoError(t, err) } diff --git a/cli/install_ee/install_ee_test.go b/cli/install_ee/install_ee_test.go index 855d8a8cf..31a147869 100644 --- a/cli/install_ee/install_ee_test.go +++ b/cli/install_ee/install_ee_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/tarantool/tt/cli/util" "github.com/tarantool/tt/cli/version" ) @@ -30,9 +31,34 @@ func TestGetVersions(t *testing.T) { err: fmt.Errorf("no packages for this OS"), } - inputData1 := []byte(`tarantool-enterprise-bundle-1.10.10-` + - `52-g0df29b137-r419.tar.gz 2021-08-18 ` + + arch, err := util.GetArch() + assert.Nil(err) + + osType, err := util.GetOs() + assert.Nil(err) + inputData1 := []byte(``) + osName := "" + switch osType { + case util.OsLinux: + // Bundles without specifying the architecture are all x86_64. + if arch == "x86_64" { + arch = "" + } + case util.OsMacos: + // Bundles without specifying the architecture are all x86_64. + if arch == "x86_64" { + osName = "-macosx-" + arch = "x86_64" + } else { + osName = "-macosx-" + } + } + + inputData1 = []byte(`tarantool-enterprise-bundle-1.10.10-` + + `52-g0df29b137-r419` + osName + arch + `.tar.gz` + + ` 2021-08-18 ` + `15:56:04 260136444`) testCases[getVersionsInputValue{data: &inputData1}] = @@ -47,7 +73,8 @@ func TestGetVersions(t *testing.T) { Release: version.Release{Type: version.TypeRelease}, Hash: "g0df29b137", Str: "1.10.10-52-g0df29b137-r419", - Tarball: "tarantool-enterprise-bundle-1.10.10-52-g0df29b137-r419.tar.gz", + Tarball: "tarantool-enterprise-bundle-1.10.10-52-g0df29b137-r419" + + osName + arch + ".tar.gz", }, }, err: nil, diff --git a/cli/util/util.go b/cli/util/util.go index 36062b5fb..00eb83803 100644 --- a/cli/util/util.go +++ b/cli/util/util.go @@ -665,8 +665,14 @@ func ExecuteCommandStdin(program string, isVerbose bool, logFile *os.File, workD cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr } else { - cmd.Stdout = logFile - cmd.Stderr = logFile + if logFile != nil { + cmd.Stdout = logFile + cmd.Stderr = logFile + } else { + cmd.Stdout = ioutil.Discard + cmd.Stderr = ioutil.Discard + } + } if workDir == "" { workDir, _ = os.Getwd()