github.com/jenkins-x/jx/v2@v2.1.155/pkg/util/files_test.go (about)

     1  // +build unit
     2  
     3  package util_test
     4  
     5  import (
     6  	"fmt"
     7  	"io/ioutil"
     8  	"os"
     9  	"path/filepath"
    10  	"sort"
    11  	"testing"
    12  
    13  	"github.com/stretchr/testify/assert"
    14  
    15  	"github.com/jenkins-x/jx/v2/pkg/util"
    16  	"github.com/stretchr/testify/require"
    17  )
    18  
    19  func TestGlobFiles(t *testing.T) {
    20  	t.Parallel()
    21  
    22  	files := []string{}
    23  	fn := func(name string) error {
    24  		if util.StringArrayIndex(files, name) < 0 {
    25  			files = append(files, name)
    26  		}
    27  		return nil
    28  	}
    29  
    30  	/*	pwd, err := os.Getwd()
    31  		require.NoError(t, err)
    32  		t.Logf("Current dir is %s\n", pwd)
    33  	*/
    34  	err := util.GlobAllFiles("", "test_data/glob_test/*", fn)
    35  	require.NoError(t, err)
    36  
    37  	sort.Strings(files)
    38  	expected := []string{
    39  		filepath.Join("test_data", "glob_test", "artifacts", "goodbye.txt"),
    40  		filepath.Join("test_data", "glob_test", "hello.txt"),
    41  	}
    42  
    43  	assert.Equal(t, expected, files, "globbed files")
    44  }
    45  
    46  func TestDeleteDirContents(t *testing.T) {
    47  	t.Parallel()
    48  
    49  	tmpDir, err := ioutil.TempDir("", "TestDeleteDirContents")
    50  	require.NoError(t, err, "Failed to create temporary directory.")
    51  	defer func() {
    52  		err = os.RemoveAll(tmpDir)
    53  	}()
    54  
    55  	// Various types
    56  	var testFileNames = []string{
    57  		"file1",
    58  		"file2.out",
    59  		"file.3.ex-tension",
    60  	}
    61  	for _, filename := range testFileNames {
    62  		//Validate filename deletion as a file, directory, and non-empty directory.
    63  		filePath := filepath.Join(tmpDir, filename)
    64  		ioutil.WriteFile(filePath, []byte(filename), os.ModePerm)
    65  		dirPath := filepath.Join(tmpDir, filename+"-dir")
    66  		os.Mkdir(dirPath, os.ModeDir)
    67  		fileInDirPath := filepath.Join(dirPath, filename)
    68  		ioutil.WriteFile(fileInDirPath, []byte(filename), os.ModePerm)
    69  	}
    70  
    71  	//delete contents
    72  	util.DeleteDirContents(tmpDir)
    73  
    74  	//check dir still exists.
    75  	_, err = os.Stat(tmpDir)
    76  	require.NoError(t, err, "Directory has been deleted.")
    77  
    78  	//check empty
    79  	remainingFiles, err := filepath.Glob(filepath.Join(tmpDir, "*"))
    80  	assert.Equal(t, 0, len(remainingFiles),
    81  		fmt.Sprintf("Expected tmp dir %s to be empty, but contains %v.", tmpDir, remainingFiles))
    82  
    83  }
    84  
    85  func TestDeleteDirContentsExcept(t *testing.T) {
    86  	t.Parallel()
    87  
    88  	tmpDir, err := ioutil.TempDir("", "TestDeleteDirContents")
    89  	require.NoError(t, err, "Failed to create temporary directory.")
    90  	defer func() {
    91  		err = os.RemoveAll(tmpDir)
    92  	}()
    93  
    94  	// Various types
    95  	var testFileNames = []string{
    96  		"file1",
    97  		"file2.out",
    98  		"file.3.ex-tension",
    99  	}
   100  	for _, filename := range testFileNames {
   101  		//Validate filename deletion as a file, directory, and non-empty directory.
   102  		filePath := filepath.Join(tmpDir, filename)
   103  		ioutil.WriteFile(filePath, []byte(filename), os.ModePerm)
   104  		dirPath := filepath.Join(tmpDir, filename+"-dir")
   105  		os.Mkdir(dirPath, os.ModeDir)
   106  		fileInDirPath := filepath.Join(dirPath, filename)
   107  		ioutil.WriteFile(fileInDirPath, []byte(filename), os.ModePerm)
   108  	}
   109  
   110  	//delete contents
   111  	util.DeleteDirContentsExcept(tmpDir, "file1")
   112  
   113  	//check dir still exists.
   114  	_, err = os.Stat(tmpDir)
   115  	require.NoError(t, err, "Directory has been deleted.")
   116  
   117  	//check empty
   118  	remainingFiles, err := filepath.Glob(filepath.Join(tmpDir, "*"))
   119  	assert.Equal(t, 1, len(remainingFiles),
   120  		fmt.Sprintf("Expected tmp dir %s to be empty, but contains %v.", tmpDir, remainingFiles))
   121  
   122  }
   123  
   124  func TestToValidFileSystemName(t *testing.T) {
   125  	assert.Equal(t, util.ToValidFileSystemName("x.y/z"), "x_y_z")
   126  }
   127  
   128  func Test_FileExists_for_non_existing_file_returns_false(t *testing.T) {
   129  	exists, err := util.FileExists("/foo/bar")
   130  	assert.NoError(t, err)
   131  	assert.False(t, exists)
   132  }
   133  
   134  func Test_FileExists_for_existing_file_returns_true(t *testing.T) {
   135  	tmpDir, err := ioutil.TempDir("", "Test_FileExists_for_existing_file_returns_true")
   136  	require.NoError(t, err, "failed to create temporary directory")
   137  	defer func() {
   138  		_ = os.RemoveAll(tmpDir)
   139  	}()
   140  
   141  	data := []byte("hello\nworld\n")
   142  	testFile := filepath.Join(tmpDir, "hello.txt")
   143  	err = ioutil.WriteFile(testFile, data, 0600)
   144  	require.NoError(t, err, "failed to create test file %s", testFile)
   145  
   146  	exists, err := util.FileExists(testFile)
   147  	assert.NoError(t, err)
   148  	assert.True(t, exists)
   149  }
   150  
   151  func Test_FileExists_for_existing_directory_returns_false(t *testing.T) {
   152  	tmpDir, err := ioutil.TempDir("", "Test_FileExists_for_existing_file_returns_true")
   153  	require.NoError(t, err, "failed to create temporary directory")
   154  	defer func() {
   155  		_ = os.RemoveAll(tmpDir)
   156  	}()
   157  
   158  	exists, err := util.FileExists(tmpDir)
   159  	assert.NoError(t, err)
   160  	assert.False(t, exists)
   161  }