Skip to content

Commit

Permalink
fix: repo structure check unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszbarwicki committed Jan 30, 2024
1 parent 31fbee9 commit 1a3b084
Showing 1 changed file with 34 additions and 28 deletions.
62 changes: 34 additions & 28 deletions release-automation/internal/repo/repo_structure_exists_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ package repo
import (
"os"
"testing"

"path"
"tractusx-release-automation/internal/filesystem"
)

Expand All @@ -44,33 +44,32 @@ var listOfDirsToBeCreated = []string{
const metadataTestFile = "./test/metadata_test_template.yaml"

func TestShouldPassIfRepoStructureExistsWithoutOptional(t *testing.T) {
setEnv(t)
defer os.Remove(".tractusx")
dir := t.TempDir()
setEnv(dir, t)

filesystem.CreateFiles(listOfFilesToBeCreated)
filesystem.CreateDirs(listOfDirsToBeCreated)
filesystem.CreateFiles(createFilesPaths(dir, listOfFilesToBeCreated))
filesystem.CreateDirs(createFilesPaths(dir, listOfDirsToBeCreated))

repostructureTest := NewRepoStructureExists("./")
result := repostructureTest.Test()
filesystem.CleanFiles(append(listOfFilesToBeCreated, listOfDirsToBeCreated...))
repoStructureTest := NewRepoStructureExists(dir)
result := repoStructureTest.Test()

if !result.Passed {
t.Errorf("Structure exists with optional files, but test still fails.")
}
}

func TestShouldPassIfRepoStructureExistsWithOptional(t *testing.T) {
setEnv(t)
defer os.Remove(".tractusx")
dir := t.TempDir()
setEnv(dir, t)

listOfFilesToBeCreated = append(listOfFilesToBeCreated, []string{"INSTALL.md", "AUTHORS.md"}...)

filesystem.CreateFiles(listOfFilesToBeCreated)
filesystem.CreateDirs(listOfDirsToBeCreated)
filesystem.CreateFiles(createFilesPaths(dir, listOfFilesToBeCreated))
filesystem.CreateDirs(createFilesPaths(dir, listOfDirsToBeCreated))


repostructureTest := NewRepoStructureExists("./")
result := repostructureTest.Test()
filesystem.CleanFiles(append(listOfFilesToBeCreated, listOfDirsToBeCreated...))
repoStructureTest := NewRepoStructureExists(dir)
result := repoStructureTest.Test()

if !result.Passed {
t.Errorf("Structure exists without optional files, but test still fails.")
Expand All @@ -79,37 +78,36 @@ func TestShouldPassIfRepoStructureExistsWithOptional(t *testing.T) {
}

func TestShouldFailIfRepoStructureIsMissing(t *testing.T) {
setEnv(t)
defer os.Remove(".tractusx")
dir := t.TempDir()
setEnv(dir, t)

repostructureTest := NewRepoStructureExists("./")
repoStructureTest := NewRepoStructureExists(dir)

result := repostructureTest.Test()
result := repoStructureTest.Test()

if result.Passed {
t.Errorf("RepoStructureExist should fail if repo structure exists.")
}
}

func TestShouldPassWithMultipleDependenciesFiles(t *testing.T) {
setEnv(t)
defer os.Remove(".tractusx")
dir := t.TempDir()
setEnv(dir, t)

newListOfFilesToBeCreated := append(listOfFilesToBeCreated[:len(listOfFilesToBeCreated)-1], []string{"DEPENDENCIES_FRONTEND", "DEPENDENCIES_BACKEND"}...)
filesystem.CreateFiles(newListOfFilesToBeCreated)
filesystem.CreateDirs(listOfDirsToBeCreated)
filesystem.CreateFiles(createFilesPaths(dir, newListOfFilesToBeCreated))
filesystem.CreateDirs(createFilesPaths(dir, listOfDirsToBeCreated))

repostructureTest := NewRepoStructureExists("./")
result := repostructureTest.Test()
filesystem.CleanFiles(append(newListOfFilesToBeCreated, listOfDirsToBeCreated...))
repoStructureTest := NewRepoStructureExists(dir)
result := repoStructureTest.Test()

if !result.Passed {
t.Errorf("There is multiple DEPENDENCIES files, test should pass.")
}
}

func setEnv(t *testing.T) {
copyTemplateFileTo(".tractusx", t)
func setEnv(dir string, t *testing.T) {
copyTemplateFileTo(path.Join(dir,".tractusx"), t)
_ = os.Setenv("GITHUB_REPOSITORY", "eclipse-tractusx/sig-infra")
_ = os.Setenv("GITHUB_REPOSITORY_OWNER", "tester")
}
Expand All @@ -124,3 +122,11 @@ func copyTemplateFileTo(path string, t *testing.T) {
t.Errorf("Could not copy template file to designated path")
}
}

func createFilesPaths(dir string, files []string) []string {
fullPaths := []string{}
for _, file := range files {
fullPaths = append(fullPaths, path.Join(dir, file))
}
return fullPaths
}

0 comments on commit 1a3b084

Please sign in to comment.