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

     1  package integration
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  	"runtime"
     7  	"strings"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/ActiveState/cli/internal/testhelpers/suite"
    12  
    13  	"github.com/ActiveState/cli/internal/constants"
    14  	"github.com/ActiveState/cli/internal/environment"
    15  	"github.com/ActiveState/cli/internal/fileutils"
    16  	"github.com/ActiveState/cli/internal/testhelpers/e2e"
    17  	"github.com/ActiveState/cli/internal/testhelpers/tagsuite"
    18  	"github.com/ActiveState/cli/pkg/project"
    19  	"github.com/ActiveState/cli/pkg/projectfile"
    20  )
    21  
    22  type EditIntegrationTestSuite struct {
    23  	tagsuite.Suite
    24  }
    25  
    26  func (suite *EditIntegrationTestSuite) setup() (*e2e.Session, e2e.SpawnOptSetter) {
    27  	ts := e2e.New(suite.T(), false)
    28  
    29  	root := environment.GetRootPathUnsafe()
    30  	editorScript := filepath.Join(root, "test", "integration", "assets", "editor", "main.go")
    31  
    32  	target := filepath.Join(ts.Dirs.Work, "editor", "main.go")
    33  	err := fileutils.CopyFile(editorScript, target)
    34  	suite.Require().NoError(err)
    35  
    36  	configFileContent := strings.TrimSpace(`
    37  project: "https://platform.activestate.com/EditOrg/EditProject"
    38  scripts:
    39    - name: test-script
    40      value: echo hello test
    41  `)
    42  	ts.PrepareActiveStateYAML(configFileContent)
    43  
    44  	editorScriptDir := filepath.Join(ts.Dirs.Work, "editor")
    45  
    46  	var extension string
    47  	if runtime.GOOS == "windows" {
    48  		extension = ".exe"
    49  	}
    50  	cp := ts.SpawnCmdWithOpts(
    51  		"go",
    52  		e2e.OptArgs("build", "-o", "editor"+extension, target),
    53  		e2e.OptWD(editorScriptDir),
    54  	)
    55  	cp.ExpectExitCode(0)
    56  
    57  	suite.Require().FileExists(filepath.Join(editorScriptDir, "editor"+extension))
    58  	return ts, e2e.OptAppendEnv(fmt.Sprintf("EDITOR=%s", filepath.Join(editorScriptDir, "editor"+extension)))
    59  }
    60  
    61  func (suite *EditIntegrationTestSuite) TearDownTest() {
    62  	projectfile.Reset()
    63  }
    64  
    65  func (suite *EditIntegrationTestSuite) TestEdit() {
    66  	suite.OnlyRunForTags(tagsuite.Edit)
    67  	ts, env := suite.setup()
    68  	defer ts.Close()
    69  	cp := ts.SpawnWithOpts(e2e.OptArgs("scripts", "edit", "test-script"), env)
    70  	cp.Expect("Watching file changes")
    71  	cp.Expect("Script changes detected")
    72  	cp.SendLine("Y")
    73  	cp.ExpectExitCode(0)
    74  	ts.IgnoreLogErrors() // ignore EditProject does not exist API errors
    75  }
    76  
    77  func (suite *EditIntegrationTestSuite) TestEdit_NonInteractive() {
    78  	suite.OnlyRunForTags(tagsuite.Edit)
    79  	if runtime.GOOS == "windows" && e2e.RunningOnCI() {
    80  		suite.T().Skip("Windows CI does not support ctrl-c interrupts.")
    81  	}
    82  	ts, env := suite.setup()
    83  	defer ts.Close()
    84  	extraEnv := e2e.OptAppendEnv(constants.NonInteractiveEnvVarName + "=true")
    85  
    86  	cp := ts.SpawnWithOpts(e2e.OptArgs("scripts", "edit", "test-script"), env, extraEnv)
    87  	cp.Expect("Watching file changes")
    88  	// Can't consistently get this line detected on CI
    89  	cp.Expect("Script changes detected")
    90  	cp.SendCtrlC()
    91  	cp.Wait()
    92  
    93  	ts.IgnoreLogErrors() // ignore EditProject does not exist API errors
    94  }
    95  
    96  func (suite *EditIntegrationTestSuite) TestEdit_UpdateCorrectPlatform() {
    97  	suite.OnlyRunForTags(tagsuite.Edit)
    98  
    99  	ts, env := suite.setup()
   100  	defer ts.Close()
   101  	cp := ts.SpawnWithOpts(
   102  		e2e.OptArgs("scripts", "edit", "test-script"),
   103  		e2e.OptWD(ts.Dirs.Work),
   104  		env,
   105  	)
   106  	cp.Expect("(Y/n)")
   107  	cp.SendLine("Y")
   108  	cp.ExpectExitCode(0)
   109  
   110  	time.Sleep(time.Second * 2) // let CI env catch up
   111  
   112  	pj, err := project.FromPath(ts.Dirs.Work)
   113  	suite.Require().NoError(err)
   114  
   115  	s := pj.ScriptByName("test-script")
   116  	suite.Require().NotNil(s, "test-script should not be empty")
   117  	v, err := s.Value()
   118  	suite.Require().NoError(err)
   119  	suite.Contains(v, "more info!", "Output of edit command:\n%s", cp.Output())
   120  
   121  	ts.IgnoreLogErrors() // ignore EditProject does not exist API errors
   122  }
   123  
   124  func TestEditIntegrationTestSuite(t *testing.T) {
   125  	suite.Run(t, new(EditIntegrationTestSuite))
   126  }