fix tests

This commit is contained in:
Amir Malka
2022-07-17 18:35:33 +03:00
parent a6bae01476
commit 44f0473a09
2 changed files with 31 additions and 20 deletions

View File

@@ -13,11 +13,13 @@ import (
"github.com/stretchr/testify/suite"
)
var TEST_REPOS = [...]string{"localrepo", "withoutremotes"}
type LocalGitRepositoryTestSuite struct {
suite.Suite
archive *zip.ReadCloser
gitRepositoryPath string
destinationPath string
archives map[string]*zip.ReadCloser
gitRepositoryPaths map[string]string
destinationPath string
}
func unzipFile(zipPath, destinationFolder string) (*zip.ReadCloser, error) {
@@ -62,17 +64,21 @@ func unzipFile(zipPath, destinationFolder string) (*zip.ReadCloser, error) {
}
func (s *LocalGitRepositoryTestSuite) SetupSuite() {
zippedFixturePath := filepath.Join(".", "testdata", "localrepo.git")
s.archives = make(map[string]*zip.ReadCloser)
s.gitRepositoryPaths = make(map[string]string)
destinationPath := filepath.Join(".", "testdata", "temp")
gitRepositoryPath := filepath.Join(destinationPath, "localrepo")
s.destinationPath = destinationPath
os.RemoveAll(destinationPath)
archive, err := unzipFile(zippedFixturePath, destinationPath)
for _, repo := range TEST_REPOS {
zippedFixturePath := filepath.Join(".", "testdata", repo+".git")
gitRepositoryPath := filepath.Join(destinationPath, repo)
archive, err := unzipFile(zippedFixturePath, destinationPath)
if err == nil {
s.archive = archive
s.gitRepositoryPath = gitRepositoryPath
s.destinationPath = destinationPath
if err == nil {
s.archives[repo] = archive
s.gitRepositoryPaths[repo] = gitRepositoryPath
}
}
}
@@ -81,9 +87,14 @@ func TestLocalGitRepositoryTestSuite(t *testing.T) {
}
func (s *LocalGitRepositoryTestSuite) TearDownSuite() {
if s.archive != nil {
s.archive.Close()
if s.archives != nil {
for _, archive := range s.archives {
if archive != nil {
archive.Close()
}
}
}
os.RemoveAll(s.destinationPath)
}
@@ -94,19 +105,19 @@ func (s *LocalGitRepositoryTestSuite) TestInvalidRepositoryPath() {
}
func (s *LocalGitRepositoryTestSuite) TestRepositoryWithoutRemotes() {
if _, err := NewLocalGitRepository("/Users/amirmalka/dev/gitwithoutremote"); s.Error(err) {
if _, err := NewLocalGitRepository(s.gitRepositoryPaths["withoutremotes"]); s.Error(err) {
s.Equal("no remotes found", err.Error())
}
}
func (s *LocalGitRepositoryTestSuite) TestGetBranchName() {
if localRepo, err := NewLocalGitRepository(s.gitRepositoryPath); s.NoError(err) {
if localRepo, err := NewLocalGitRepository(s.gitRepositoryPaths["localrepo"]); s.NoError(err) {
s.Equal("master", localRepo.GetBranchName())
}
}
func (s *LocalGitRepositoryTestSuite) TestGetName() {
if localRepo, err := NewLocalGitRepository(s.gitRepositoryPath); s.NoError(err) {
if localRepo, err := NewLocalGitRepository(s.gitRepositoryPaths["localrepo"]); s.NoError(err) {
if name, err := localRepo.GetName(); s.NoError(err) {
s.Equal("localrepo", name)
}
@@ -115,7 +126,7 @@ func (s *LocalGitRepositoryTestSuite) TestGetName() {
}
func (s *LocalGitRepositoryTestSuite) TestGetOriginUrl() {
if localRepo, err := NewLocalGitRepository(s.gitRepositoryPath); s.NoError(err) {
if localRepo, err := NewLocalGitRepository(s.gitRepositoryPaths["localrepo"]); s.NoError(err) {
if url, err := localRepo.GetRemoteUrl(); s.NoError(err) {
s.Equal("git@github.com:testuser/localrepo", url)
}
@@ -123,7 +134,7 @@ func (s *LocalGitRepositoryTestSuite) TestGetOriginUrl() {
}
func (s *LocalGitRepositoryTestSuite) TestGetLastCommit() {
if localRepo, err := NewLocalGitRepository(s.gitRepositoryPath); s.NoError(err) {
if localRepo, err := NewLocalGitRepository(s.gitRepositoryPaths["localrepo"]); s.NoError(err) {
if commit, err := localRepo.GetLastCommit(); s.NoError(err) {
s.Equal("7e09312b8017695fadcd606882e3779f10a5c832", commit.SHA)
s.Equal("Amir Malka", commit.Author.Name)
@@ -136,7 +147,7 @@ func (s *LocalGitRepositoryTestSuite) TestGetLastCommit() {
func (s *LocalGitRepositoryTestSuite) TestGetFileLastCommit() {
s.Run("fileA", func() {
if localRepo, err := NewLocalGitRepository(s.gitRepositoryPath); s.NoError(err) {
if localRepo, err := NewLocalGitRepository(s.gitRepositoryPaths["localrepo"]); s.NoError(err) {
if runtime.GOOS != "windows" {
if commit, err := localRepo.GetFileLastCommit("fileA"); s.NoError(err) {
s.Equal("9fae4be19624297947d2b605cefbff516628612d", commit.SHA)
@@ -150,7 +161,7 @@ func (s *LocalGitRepositoryTestSuite) TestGetFileLastCommit() {
})
s.Run("fileB", func() {
if localRepo, err := NewLocalGitRepository(s.gitRepositoryPath); s.NoError(err) {
if localRepo, err := NewLocalGitRepository(s.gitRepositoryPaths["localrepo"]); s.NoError(err) {
if runtime.GOOS != "windows" {
if commit, err := localRepo.GetFileLastCommit("dirA/fileB"); s.NoError(err) {
s.Equal("7e09312b8017695fadcd606882e3779f10a5c832", commit.SHA)

BIN
core/cautils/testdata/withoutremotes.git vendored Normal file

Binary file not shown.