diff --git a/src/Makefile b/src/Makefile index c234908..75c1267 100644 --- a/src/Makefile +++ b/src/Makefile @@ -70,6 +70,11 @@ clean: @rm -rf workspace_* .PHONY: test +test_%: + go test oc-deploy/$(subst test_,,$@) -coverprofile=.coverage.out -v + @go tool cover -html=.coverage.out -o .coverage.html + test: @go test ./... -coverprofile=.coverage.out -v go tool cover -html=.coverage.out -o .coverage.html + diff --git a/src/utils/download.go b/src/utils/download.go index 31f500a..ef70df5 100644 --- a/src/utils/download.go +++ b/src/utils/download.go @@ -1,7 +1,6 @@ package utils import ( - // "fmt" "os" "io" "path" @@ -56,7 +55,7 @@ func ExtractTarGz(dest string, gzipStream io.Reader) error { // return err // } case tar.TypeReg: - outName := dest + "/" + path.Base( header.Name) + outName := dest + "/" + path.Base(header.Name) outFile, _ := os.Create(outName) if err != nil { return err diff --git a/src/utils/download_test.go b/src/utils/download_test.go index 66f56ba..54c83ce 100644 --- a/src/utils/download_test.go +++ b/src/utils/download_test.go @@ -1,8 +1,7 @@ package utils -// https://pkg.go.dev/github.com/stretchr/testify/assert#Nilf - import ( + "os" "path/filepath" "testing" @@ -26,3 +25,27 @@ func TestDownload(t *testing.T) { assert.Nilf(t, err, "error message %s", url) assert.FileExists(t, dest, "DownloadFromUrl error") } + +func TestExtractTarGz(t *testing.T) { + + dest := filepath.Join(TEST_DEST_DIR, "extract") + os.MkdirAll(dest, os.ModePerm) + + src := filepath.Join(TEST_SRC_DIR, "fichier1.tgz") + file, _ := os.Open(src) + + err := ExtractTarGz(dest, file) + assert.Nilf(t, err, "error message %s", src) + assert.FileExists(t, filepath.Join(dest, "fichier1"), "TestExtractTarGz error") +} + +func TestExtractTarGzErr(t *testing.T) { + + dest := filepath.Join(TEST_DEST_DIR, "extract") + + src := filepath.Join(TEST_SRC_DIR, "fichier1") + file, _ := os.Open(src) + + err := ExtractTarGz(dest, file) + assert.NotNilf(t, err, "error message %s", src) +} \ No newline at end of file diff --git a/src/utils/main_test.go b/src/utils/main_test.go index 2128336..c34f571 100644 --- a/src/utils/main_test.go +++ b/src/utils/main_test.go @@ -1,23 +1,25 @@ package utils +// https://pkg.go.dev/github.com/stretchr/testify/assert + import ( - "os" - "testing" - "path/filepath" + "os" + "testing" + "path/filepath" ) var TEST_DEST_DIR = "../wrk_utils" var TEST_SRC_DIR = filepath.Join("../../test", "utils") func TestMain(m *testing.M) { - folderPath := TEST_DEST_DIR + folderPath := TEST_DEST_DIR - os.RemoveAll(folderPath) - os.MkdirAll(folderPath, os.ModePerm) + os.RemoveAll(folderPath) + os.MkdirAll(folderPath, os.ModePerm) - // call flag.Parse() here if TestMain uses flags - exitCode := m.Run() + // call flag.Parse() here if TestMain uses flags + exitCode := m.Run() - os.RemoveAll(folderPath) - os.Exit(exitCode) + os.RemoveAll(folderPath) + os.Exit(exitCode) } \ No newline at end of file diff --git a/src/utils/slice_test.go b/src/utils/slice_test.go new file mode 100644 index 0000000..18c462f --- /dev/null +++ b/src/utils/slice_test.go @@ -0,0 +1,22 @@ +package utils + +import ( + "testing" + "github.com/stretchr/testify/assert" +) + +func TestSliceInStringExist(t *testing.T) { + liste := []string{"text1", "text2"} + + res := StringInSlice("text1", liste) + + assert.Truef(t, res, "error message %s", "text1") +} + +func TestSliceInStringNotExist(t *testing.T) { + liste := []string{"text1", "text2"} + + res := StringInSlice("text3", liste) + + assert.Falsef(t, res, "error message %s", "text3") +} diff --git a/test/utils/fichier1.tgz b/test/utils/fichier1.tgz new file mode 100644 index 0000000..8270257 Binary files /dev/null and b/test/utils/fichier1.tgz differ