github.com/nilium/gitlab-runner@v12.5.0+incompatible/commands/helpers/cache_init_test.go (about)

     1  package helpers
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path"
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  	"github.com/urfave/cli"
    12  
    13  	"gitlab.com/gitlab-org/gitlab-runner/helpers"
    14  )
    15  
    16  func newCacheInitTestApp() *cli.App {
    17  	cmd := &CacheInitCommand{}
    18  	app := cli.NewApp()
    19  	app.Name = path.Base(os.Args[0])
    20  	app.Commands = append(app.Commands, cli.Command{
    21  		Name:   "cache-init",
    22  		Action: cmd.Execute,
    23  	})
    24  
    25  	return app
    26  }
    27  
    28  func TestCacheInit(t *testing.T) {
    29  	// Specifically test a dir name with spaces.
    30  	dir, err := ioutil.TempDir("", "Test Cache Chmod")
    31  	require.NoError(t, err)
    32  
    33  	defer os.Remove(dir)
    34  
    35  	// Make sure that the mode is not the expect 0777.
    36  	err = os.Chmod(dir, 0600)
    37  	require.NoError(t, err)
    38  
    39  	// Start a new cli with the arguments for the command.
    40  	args := os.Args[0:1]
    41  	args = append(args, "cache-init", dir)
    42  
    43  	err = newCacheInitTestApp().Run(args)
    44  	require.NoError(t, err)
    45  
    46  	info, err := os.Stat(dir)
    47  	require.NoError(t, err)
    48  
    49  	assert.Equal(t, os.ModeDir+os.ModePerm, info.Mode())
    50  }
    51  
    52  func TestCacheInit_NoArguments(t *testing.T) {
    53  	removeHook := helpers.MakeFatalToPanic()
    54  	defer removeHook()
    55  
    56  	args := os.Args[0:1]
    57  	args = append(args, "cache-init")
    58  
    59  	assert.Panics(t, func() {
    60  		newCacheInitTestApp().Run(args)
    61  	})
    62  }