github.com/fluffynuts/lazygit@v0.8.1/pkg/commands/os_test.go (about)

     1  package commands
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"os/exec"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  // TestOSCommandRunCommandWithOutput is a function.
    13  func TestOSCommandRunCommandWithOutput(t *testing.T) {
    14  	type scenario struct {
    15  		command string
    16  		test    func(string, error)
    17  	}
    18  
    19  	scenarios := []scenario{
    20  		{
    21  			"echo -n '123'",
    22  			func(output string, err error) {
    23  				assert.NoError(t, err)
    24  				assert.EqualValues(t, "123", output)
    25  			},
    26  		},
    27  		{
    28  			"rmdir unexisting-folder",
    29  			func(output string, err error) {
    30  				assert.Regexp(t, "rmdir.*unexisting-folder.*", err.Error())
    31  			},
    32  		},
    33  	}
    34  
    35  	for _, s := range scenarios {
    36  		s.test(NewDummyOSCommand().RunCommandWithOutput(s.command))
    37  	}
    38  }
    39  
    40  // TestOSCommandRunCommand is a function.
    41  func TestOSCommandRunCommand(t *testing.T) {
    42  	type scenario struct {
    43  		command string
    44  		test    func(error)
    45  	}
    46  
    47  	scenarios := []scenario{
    48  		{
    49  			"rmdir unexisting-folder",
    50  			func(err error) {
    51  				assert.Regexp(t, "rmdir.*unexisting-folder.*", err.Error())
    52  			},
    53  		},
    54  	}
    55  
    56  	for _, s := range scenarios {
    57  		s.test(NewDummyOSCommand().RunCommand(s.command))
    58  	}
    59  }
    60  
    61  // TestOSCommandOpenFile is a function.
    62  func TestOSCommandOpenFile(t *testing.T) {
    63  	type scenario struct {
    64  		filename string
    65  		command  func(string, ...string) *exec.Cmd
    66  		test     func(error)
    67  	}
    68  
    69  	scenarios := []scenario{
    70  		{
    71  			"test",
    72  			func(name string, arg ...string) *exec.Cmd {
    73  				return exec.Command("exit", "1")
    74  			},
    75  			func(err error) {
    76  				assert.Error(t, err)
    77  			},
    78  		},
    79  		{
    80  			"test",
    81  			func(name string, arg ...string) *exec.Cmd {
    82  				assert.Equal(t, "open", name)
    83  				assert.Equal(t, []string{"test"}, arg)
    84  				return exec.Command("echo")
    85  			},
    86  			func(err error) {
    87  				assert.NoError(t, err)
    88  			},
    89  		},
    90  		{
    91  			"filename with spaces",
    92  			func(name string, arg ...string) *exec.Cmd {
    93  				assert.Equal(t, "open", name)
    94  				assert.Equal(t, []string{"filename with spaces"}, arg)
    95  				return exec.Command("echo")
    96  			},
    97  			func(err error) {
    98  				assert.NoError(t, err)
    99  			},
   100  		},
   101  	}
   102  
   103  	for _, s := range scenarios {
   104  		OSCmd := NewDummyOSCommand()
   105  		OSCmd.command = s.command
   106  		OSCmd.Config.GetUserConfig().Set("os.openCommand", "open {{filename}}")
   107  
   108  		s.test(OSCmd.OpenFile(s.filename))
   109  	}
   110  }
   111  
   112  // TestOSCommandEditFile is a function.
   113  func TestOSCommandEditFile(t *testing.T) {
   114  	type scenario struct {
   115  		filename           string
   116  		command            func(string, ...string) *exec.Cmd
   117  		getenv             func(string) string
   118  		getGlobalGitConfig func(string) (string, error)
   119  		test               func(*exec.Cmd, error)
   120  	}
   121  
   122  	scenarios := []scenario{
   123  		{
   124  			"test",
   125  			func(name string, arg ...string) *exec.Cmd {
   126  				return exec.Command("exit", "1")
   127  			},
   128  			func(env string) string {
   129  				return ""
   130  			},
   131  			func(cf string) (string, error) {
   132  				return "", nil
   133  			},
   134  			func(cmd *exec.Cmd, err error) {
   135  				assert.EqualError(t, err, "No editor defined in $VISUAL, $EDITOR, or git config")
   136  			},
   137  		},
   138  		{
   139  			"test",
   140  			func(name string, arg ...string) *exec.Cmd {
   141  				if name == "which" {
   142  					return exec.Command("exit", "1")
   143  				}
   144  
   145  				assert.EqualValues(t, "nano", name)
   146  
   147  				return nil
   148  			},
   149  			func(env string) string {
   150  				return ""
   151  			},
   152  			func(cf string) (string, error) {
   153  				return "nano", nil
   154  			},
   155  			func(cmd *exec.Cmd, err error) {
   156  				assert.NoError(t, err)
   157  			},
   158  		},
   159  		{
   160  			"test",
   161  			func(name string, arg ...string) *exec.Cmd {
   162  				if name == "which" {
   163  					return exec.Command("exit", "1")
   164  				}
   165  
   166  				assert.EqualValues(t, "nano", name)
   167  
   168  				return nil
   169  			},
   170  			func(env string) string {
   171  				if env == "VISUAL" {
   172  					return "nano"
   173  				}
   174  
   175  				return ""
   176  			},
   177  			func(cf string) (string, error) {
   178  				return "", nil
   179  			},
   180  			func(cmd *exec.Cmd, err error) {
   181  				assert.NoError(t, err)
   182  			},
   183  		},
   184  		{
   185  			"test",
   186  			func(name string, arg ...string) *exec.Cmd {
   187  				if name == "which" {
   188  					return exec.Command("exit", "1")
   189  				}
   190  
   191  				assert.EqualValues(t, "emacs", name)
   192  
   193  				return nil
   194  			},
   195  			func(env string) string {
   196  				if env == "EDITOR" {
   197  					return "emacs"
   198  				}
   199  
   200  				return ""
   201  			},
   202  			func(cf string) (string, error) {
   203  				return "", nil
   204  			},
   205  			func(cmd *exec.Cmd, err error) {
   206  				assert.NoError(t, err)
   207  			},
   208  		},
   209  		{
   210  			"test",
   211  			func(name string, arg ...string) *exec.Cmd {
   212  				if name == "which" {
   213  					return exec.Command("echo")
   214  				}
   215  
   216  				assert.EqualValues(t, "vi", name)
   217  
   218  				return nil
   219  			},
   220  			func(env string) string {
   221  				return ""
   222  			},
   223  			func(cf string) (string, error) {
   224  				return "", nil
   225  			},
   226  			func(cmd *exec.Cmd, err error) {
   227  				assert.NoError(t, err)
   228  			},
   229  		},
   230  	}
   231  
   232  	for _, s := range scenarios {
   233  		OSCmd := NewDummyOSCommand()
   234  		OSCmd.command = s.command
   235  		OSCmd.getGlobalGitConfig = s.getGlobalGitConfig
   236  		OSCmd.getenv = s.getenv
   237  
   238  		s.test(OSCmd.EditFile(s.filename))
   239  	}
   240  }
   241  
   242  // TestOSCommandQuote is a function.
   243  func TestOSCommandQuote(t *testing.T) {
   244  	osCommand := NewDummyOSCommand()
   245  
   246  	actual := osCommand.Quote("hello `test`")
   247  
   248  	expected := osCommand.Platform.escapedQuote + "hello \\`test\\`" + osCommand.Platform.escapedQuote
   249  
   250  	assert.EqualValues(t, expected, actual)
   251  }
   252  
   253  // TestOSCommandQuoteSingleQuote tests the quote function with ' quotes explicitly for Linux
   254  func TestOSCommandQuoteSingleQuote(t *testing.T) {
   255  	osCommand := NewDummyOSCommand()
   256  
   257  	osCommand.Platform.os = "linux"
   258  
   259  	actual := osCommand.Quote("hello 'test'")
   260  
   261  	expected := osCommand.Platform.fallbackEscapedQuote + "hello 'test'" + osCommand.Platform.fallbackEscapedQuote
   262  
   263  	assert.EqualValues(t, expected, actual)
   264  }
   265  
   266  // TestOSCommandQuoteDoubleQuote tests the quote function with " quotes explicitly for Linux
   267  func TestOSCommandQuoteDoubleQuote(t *testing.T) {
   268  	osCommand := NewDummyOSCommand()
   269  
   270  	osCommand.Platform.os = "linux"
   271  
   272  	actual := osCommand.Quote(`hello "test"`)
   273  
   274  	expected := osCommand.Platform.escapedQuote + "hello \"test\"" + osCommand.Platform.escapedQuote
   275  
   276  	assert.EqualValues(t, expected, actual)
   277  }
   278  
   279  // TestOSCommandUnquote is a function.
   280  func TestOSCommandUnquote(t *testing.T) {
   281  	osCommand := NewDummyOSCommand()
   282  
   283  	actual := osCommand.Unquote(`hello "test"`)
   284  
   285  	expected := "hello test"
   286  
   287  	assert.EqualValues(t, expected, actual)
   288  }
   289  
   290  // TestOSCommandFileType is a function.
   291  func TestOSCommandFileType(t *testing.T) {
   292  	type scenario struct {
   293  		path  string
   294  		setup func()
   295  		test  func(string)
   296  	}
   297  
   298  	scenarios := []scenario{
   299  		{
   300  			"testFile",
   301  			func() {
   302  				if _, err := os.Create("testFile"); err != nil {
   303  					panic(err)
   304  				}
   305  			},
   306  			func(output string) {
   307  				assert.EqualValues(t, "file", output)
   308  			},
   309  		},
   310  		{
   311  			"file with spaces",
   312  			func() {
   313  				if _, err := os.Create("file with spaces"); err != nil {
   314  					panic(err)
   315  				}
   316  			},
   317  			func(output string) {
   318  				assert.EqualValues(t, "file", output)
   319  			},
   320  		},
   321  		{
   322  			"testDirectory",
   323  			func() {
   324  				if err := os.Mkdir("testDirectory", 0644); err != nil {
   325  					panic(err)
   326  				}
   327  			},
   328  			func(output string) {
   329  				assert.EqualValues(t, "directory", output)
   330  			},
   331  		},
   332  		{
   333  			"nonExistant",
   334  			func() {},
   335  			func(output string) {
   336  				assert.EqualValues(t, "other", output)
   337  			},
   338  		},
   339  	}
   340  
   341  	for _, s := range scenarios {
   342  		s.setup()
   343  		s.test(NewDummyOSCommand().FileType(s.path))
   344  		_ = os.RemoveAll(s.path)
   345  	}
   346  }
   347  
   348  func TestOSCommandCreateTempFile(t *testing.T) {
   349  	type scenario struct {
   350  		testName string
   351  		filename string
   352  		content  string
   353  		test     func(string, error)
   354  	}
   355  
   356  	scenarios := []scenario{
   357  		{
   358  			"valid case",
   359  			"filename",
   360  			"content",
   361  			func(path string, err error) {
   362  				assert.NoError(t, err)
   363  
   364  				content, err := ioutil.ReadFile(path)
   365  				assert.NoError(t, err)
   366  
   367  				assert.Equal(t, "content", string(content))
   368  			},
   369  		},
   370  	}
   371  
   372  	for _, s := range scenarios {
   373  		t.Run(s.testName, func(t *testing.T) {
   374  			s.test(NewDummyOSCommand().CreateTempFile(s.filename, s.content))
   375  		})
   376  	}
   377  }