github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/test/integration/projects_int_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"fmt"
     5  	"runtime"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/ActiveState/cli/internal/fileutils"
    10  	"github.com/ActiveState/cli/internal/testhelpers/e2e"
    11  	"github.com/ActiveState/cli/internal/testhelpers/suite"
    12  	"github.com/ActiveState/cli/internal/testhelpers/tagsuite"
    13  )
    14  
    15  type ProjectsIntegrationTestSuite struct {
    16  	tagsuite.Suite
    17  }
    18  
    19  func (suite *ProjectsIntegrationTestSuite) TestProjects() {
    20  	suite.OnlyRunForTags(tagsuite.Projects)
    21  	ts := e2e.New(suite.T(), false)
    22  	defer ts.Close()
    23  
    24  	cp := ts.SpawnWithOpts(e2e.OptArgs("checkout", "ActiveState-CLI/small-python"))
    25  	cp.ExpectExitCode(0)
    26  	cp = ts.SpawnWithOpts(e2e.OptArgs("checkout", "ActiveState-CLI/Python3"))
    27  	cp.ExpectExitCode(0)
    28  
    29  	// Verify local checkouts and executables are grouped together under projects.
    30  	cp = ts.SpawnWithOpts(e2e.OptArgs("projects"))
    31  	cp.Expect("Python3")
    32  	cp.Expect("Local Checkout")
    33  	if runtime.GOOS != "windows" {
    34  		cp.Expect(ts.Dirs.Work)
    35  	} else {
    36  		// Windows uses the long path here.
    37  		longPath, _ := fileutils.GetLongPathName(ts.Dirs.Work)
    38  		cp.Expect(longPath)
    39  	}
    40  	cp.Expect("Executables")
    41  	cp.Expect(ts.Dirs.Cache)
    42  	cp.Expect("small-python")
    43  	cp.Expect("Local Checkout")
    44  	if runtime.GOOS != "windows" {
    45  		cp.Expect(ts.Dirs.Work)
    46  	} else {
    47  		// Windows uses the long path here.
    48  		longPath, _ := fileutils.GetLongPathName(ts.Dirs.Work)
    49  		cp.Expect(longPath)
    50  	}
    51  	cp.Expect("Executables")
    52  	cp.Expect(ts.Dirs.Cache)
    53  	cp.ExpectExitCode(0)
    54  }
    55  
    56  func (suite *ProjectsIntegrationTestSuite) TestJSON() {
    57  	suite.OnlyRunForTags(tagsuite.Projects, tagsuite.JSON)
    58  	ts := e2e.New(suite.T(), false)
    59  	defer ts.Close()
    60  
    61  	cp := ts.Spawn("checkout", "ActiveState-CLI/small-python")
    62  	cp.ExpectExitCode(0)
    63  	cp = ts.Spawn("checkout", "ActiveState-CLI/Python3")
    64  	cp.ExpectExitCode(0)
    65  	cp = ts.Spawn("projects", "-o", "json")
    66  	cp.Expect(`[{"name":`)
    67  	cp.Expect(`"local_checkouts":`)
    68  	cp.Expect(`"executables":`)
    69  	cp.ExpectExitCode(0)
    70  	AssertValidJSON(suite.T(), cp)
    71  
    72  	ts.LoginAsPersistentUser()
    73  	cp = ts.Spawn("projects", "remote", "--output", "json")
    74  	cp.Expect(`[{`)
    75  	cp.Expect(`}]`)
    76  	cp.ExpectExitCode(0)
    77  	// AssertValidJSON(suite.T(), cp) // list is too large to fit in terminal snapshot
    78  }
    79  
    80  func (suite *ProjectsIntegrationTestSuite) TestEdit_Name() {
    81  	suite.OnlyRunForTags(tagsuite.Projects)
    82  	ts := e2e.New(suite.T(), false)
    83  	defer ts.Close()
    84  
    85  	ts.LoginAsPersistentUser()
    86  
    87  	// What we expect the project name to be and what we want to change it to.
    88  	// This can change if the test failed previously.
    89  	var (
    90  		originalName = fmt.Sprintf("Edit-Test-%s", runtime.GOOS)
    91  		newName      = fmt.Sprintf("Edit-Rename-%s", runtime.GOOS)
    92  	)
    93  
    94  	cp := ts.Spawn("checkout", fmt.Sprintf("ActiveState-CLI/%s", originalName))
    95  
    96  	// If the checkout failed, it's probably because the project name was changed
    97  	// in a previous run of this test. Try again with the new name.
    98  	if strings.Contains(cp.Output(), "Could not checkout project") {
    99  		cp = ts.Spawn("checkout", fmt.Sprintf("ActiveState-CLI/%s", newName))
   100  		originalName = newName
   101  		newName = originalName
   102  	}
   103  	cp.ExpectExitCode(0)
   104  
   105  	cp = ts.Spawn("projects")
   106  	cp.Expect(originalName)
   107  	cp.ExpectExitCode(0)
   108  
   109  	cp = ts.Spawn("projects", "edit", fmt.Sprintf("ActiveState-CLI/%s", originalName), "--name", newName)
   110  	cp.Expect("You are about to edit")
   111  	cp.SendLine("y")
   112  	cp.Expect("Project edited successfully")
   113  	cp.ExpectExitCode(0)
   114  
   115  	// Verify the local checkouts have been updated
   116  	cp = ts.Spawn("projects")
   117  	cp.Expect(newName)
   118  	cp.ExpectExitCode(0)
   119  
   120  	// Change name back to original
   121  	cp = ts.Spawn("projects", "edit", fmt.Sprintf("ActiveState-CLI/%s", newName), "--name", originalName)
   122  	cp.Expect("You are about to edit")
   123  	cp.SendLine("y")
   124  	cp.Expect("Project edited successfully")
   125  	cp.ExpectExitCode(0)
   126  
   127  	// Verify the local checkouts have been updated
   128  	cp = ts.Spawn("projects")
   129  	cp.Expect(originalName)
   130  	cp.ExpectExitCode(0)
   131  }
   132  
   133  func (suite *ProjectsIntegrationTestSuite) TestEdit_Visibility() {
   134  	suite.OnlyRunForTags(tagsuite.Projects)
   135  	ts := e2e.New(suite.T(), false)
   136  	defer ts.Close()
   137  
   138  	ts.LoginAsPersistentUser()
   139  
   140  	namespace := fmt.Sprintf("ActiveState-CLI/Visibility-Test-%s", runtime.GOOS)
   141  
   142  	cp := ts.Spawn("projects", "edit", namespace, "--visibility", "private")
   143  	cp.Expect("You are about to edit")
   144  	cp.SendLine("y")
   145  	cp.Expect("Project edited successfully")
   146  	cp.ExpectExitCode(0)
   147  
   148  	ts.LogoutUser()
   149  
   150  	cp = ts.Spawn("checkout", namespace)
   151  	cp.Expect("does not exist under ActiveState-CLI")
   152  	cp.ExpectExitCode(1)
   153  	ts.IgnoreLogErrors()
   154  
   155  	ts.LoginAsPersistentUser()
   156  
   157  	cp = ts.Spawn("projects", "edit", namespace, "--visibility", "public")
   158  	cp.Expect("You are about to edit")
   159  	cp.SendLine("y")
   160  	cp.Expect("Project edited successfully")
   161  	cp.ExpectExitCode(0)
   162  }
   163  
   164  func (suite *ProjectsIntegrationTestSuite) TestMove() {
   165  	suite.OnlyRunForTags(tagsuite.Projects)
   166  	ts := e2e.New(suite.T(), false)
   167  	defer ts.Close()
   168  
   169  	ts.LoginAsPersistentUser()
   170  
   171  	// Just test interactivity, since we only have one integration test org.
   172  	cp := ts.Spawn("projects", "move", "ActiveState-CLI/small-python", "ActiveState-CLI")
   173  	cp.Expect("You are about to move")
   174  	cp.Expect("ActiveState-CLI/small-python")
   175  	cp.Expect("ActiveState-CLI")
   176  	cp.Expect("Continue? (y/N)")
   177  	cp.SendLine("n")
   178  	cp.Expect("aborted")
   179  	cp.ExpectExitCode(0)
   180  }
   181  
   182  func TestProjectsIntegrationTestSuite(t *testing.T) {
   183  	suite.Run(t, new(ProjectsIntegrationTestSuite))
   184  }