github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/internal/runbits/git/test/integration/git_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/ActiveState/cli/internal/analytics/client/blackhole"
    11  	"github.com/ActiveState/cli/internal/testhelpers/suite"
    12  	"gopkg.in/src-d/go-git.v4"
    13  	"gopkg.in/src-d/go-git.v4/plumbing/object"
    14  
    15  	"github.com/ActiveState/cli/internal/constants"
    16  	"github.com/ActiveState/cli/internal/fileutils"
    17  	"github.com/ActiveState/cli/internal/locale"
    18  	runbitsGit "github.com/ActiveState/cli/internal/runbits/git"
    19  	"github.com/ActiveState/cli/internal/testhelpers/outputhelper"
    20  	"github.com/ActiveState/cli/pkg/project"
    21  )
    22  
    23  type GitTestSuite struct {
    24  	suite.Suite
    25  	dir        string
    26  	anotherDir string
    27  }
    28  
    29  func (suite *GitTestSuite) BeforeTest(suiteName, testName string) {
    30  
    31  	var err error
    32  	suite.dir, err = os.MkdirTemp("", testName)
    33  	suite.NoError(err, "could not create a temporary directory")
    34  
    35  	repo, err := git.PlainInit(suite.dir, false)
    36  	suite.NoError(err, "could not init a new git repo")
    37  
    38  	worktree, err := repo.Worktree()
    39  	suite.NoError(err, "could not get repository worktree")
    40  
    41  	projectURL := fmt.Sprintf("https://%s/%s/%s", constants.PlatformURL, "test-owner", "test-project")
    42  
    43  	err = fileutils.WriteFile(filepath.Join(suite.dir, "activestate.yaml"), []byte("project: "+projectURL))
    44  	suite.NoError(err, "could not create a projectfile")
    45  
    46  	err = fileutils.Touch(filepath.Join(suite.dir, "test-file"))
    47  	suite.NoError(err, "could not create a temp file")
    48  
    49  	_, err = worktree.Add("test-file")
    50  	suite.NoError(err, "could not add tempfile to staging")
    51  
    52  	_, err = worktree.Add("activestate.yaml")
    53  	suite.NoError(err, "could not add projectfile to staging")
    54  
    55  	commit, err := worktree.Commit("commit for test", &git.CommitOptions{
    56  		Author: &object.Signature{
    57  			Name:  "testing",
    58  			Email: "testing@testing.org",
    59  			When:  time.Now(),
    60  		},
    61  	})
    62  	suite.NoError(err, "could not create a commit")
    63  
    64  	_, err = repo.CommitObject(commit)
    65  	suite.NoError(err, "could not commit testfile")
    66  
    67  	suite.anotherDir, err = os.MkdirTemp("", "TestMoveFiles")
    68  	suite.NoError(err, "could not create another temporary directory")
    69  }
    70  
    71  func (suite *GitTestSuite) AfterTest(suiteName, testName string) {
    72  	err := os.RemoveAll(suite.dir)
    73  	if err != nil {
    74  		fmt.Printf("WARNING: Could not remove temp dir: %s, error: %v", suite.dir, err)
    75  	}
    76  	err = os.RemoveAll(suite.anotherDir)
    77  	if err != nil {
    78  		fmt.Printf("WARNING: Could not remove temp dir: %s, error: %v", suite.dir, err)
    79  	}
    80  }
    81  
    82  func (suite *GitTestSuite) TestEnsureCorrectProject() {
    83  	err := runbitsGit.EnsureCorrectProject("test-owner", "test-project", filepath.Join(suite.dir, constants.ConfigFileName), "test-repo", outputhelper.NewCatcher(), blackhole.New())
    84  	suite.NoError(err, "projectfile URL should contain owner and name")
    85  }
    86  
    87  func (suite *GitTestSuite) TestEnsureCorrectProject_Missmatch() {
    88  	owner := "not-owner"
    89  	name := "bad-project"
    90  	projectPath := filepath.Join(suite.dir, constants.ConfigFileName)
    91  	actualCatcher := outputhelper.NewCatcher()
    92  	err := runbitsGit.EnsureCorrectProject(owner, name, projectPath, "test-repo", actualCatcher, blackhole.New())
    93  	suite.NoError(err)
    94  
    95  	proj, err := project.Parse(projectPath)
    96  	suite.NoError(err)
    97  
    98  	expectedCatcher := outputhelper.NewCatcher()
    99  	expectedCatcher.Notice(locale.Tr("warning_git_project_mismatch", "test-repo", project.NewNamespace(owner, name, "").String(), constants.DocumentationURLMismatch))
   100  
   101  	suite.Equal(expectedCatcher.CombinedOutput(), actualCatcher.CombinedOutput())
   102  	suite.Equal(owner, proj.Owner())
   103  	suite.Equal(name, proj.Name())
   104  }
   105  
   106  func (suite *GitTestSuite) TestMoveFiles() {
   107  	anotherDir := filepath.Join(suite.anotherDir, "anotherDir")
   108  	err := runbitsGit.MoveFiles(suite.dir, anotherDir)
   109  	suite.NoError(err, "should be able to move files wihout error")
   110  
   111  	_, err = os.Stat(filepath.Join(anotherDir, constants.ConfigFileName))
   112  	suite.NoError(err, "file should be moved")
   113  
   114  	_, err = os.Stat(filepath.Join(anotherDir, "test-file"))
   115  	suite.NoError(err, "file should be moved")
   116  }
   117  
   118  func (suite *GitTestSuite) TestMoveFilesDirNoEmpty() {
   119  	anotherDir := filepath.Join(suite.anotherDir, "anotherDir")
   120  	err := os.MkdirAll(anotherDir, 0755)
   121  	suite.NoError(err, "should be able to create another temp directory")
   122  
   123  	err = fileutils.Touch(filepath.Join(anotherDir, "file.txt"))
   124  	suite.Require().NoError(err)
   125  
   126  	err = runbitsGit.MoveFiles(suite.dir, anotherDir)
   127  	expected := locale.WrapError(err, "err_git_verify_dir", "Could not verify destination directory")
   128  	suite.EqualError(err, expected.Error())
   129  }
   130  
   131  func TestGitTestSuite(t *testing.T) {
   132  	suite.Run(t, new(GitTestSuite))
   133  }