github.com/projecteru2/core@v0.0.0-20240321043226-06bcc1c23f58/utils/file_test.go (about)

     1  package utils
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestListAllExecutableFiles(t *testing.T) {
    12  	dir, err := os.MkdirTemp(os.TempDir(), "test*")
    13  	assert.NoError(t, err)
    14  	defer os.RemoveAll(dir)
    15  
    16  	file, err := os.CreateTemp(dir, "abc")
    17  	assert.NoError(t, err)
    18  
    19  	subdir, err := os.MkdirTemp(dir, "def")
    20  	assert.NoError(t, err)
    21  
    22  	assert.NotNil(t, file)
    23  	assert.NotNil(t, subdir)
    24  
    25  	fInfo, err := os.Stat(file.Name())
    26  	assert.NoError(t, err)
    27  	assert.NotNil(t, fInfo)
    28  
    29  	assert.False(t, isExecutable(fInfo.Mode().Perm()))
    30  
    31  	os.Chmod(file.Name(), 0777)
    32  	fInfo, _ = os.Stat(file.Name())
    33  	assert.True(t, isExecutable(fInfo.Mode().Perm()))
    34  
    35  	fs, err := ListAllExecutableFiles(dir)
    36  	assert.NoError(t, err)
    37  	assert.Len(t, fs, 1)
    38  }
    39  
    40  func TestListAllShareLibFiles(t *testing.T) {
    41  	dir, err := os.MkdirTemp(os.TempDir(), "test*")
    42  	assert.NoError(t, err)
    43  	defer os.RemoveAll(dir)
    44  
    45  	_, err = os.Create(filepath.Join(dir, "abc"))
    46  	assert.NoError(t, err)
    47  
    48  	_, err = os.Create(filepath.Join(dir, "bcd.so"))
    49  	assert.NoError(t, err)
    50  
    51  	subdir, err := os.MkdirTemp(dir, "def")
    52  	assert.NoError(t, err)
    53  
    54  	_, err = os.Create(filepath.Join(subdir, "abc1"))
    55  	assert.NoError(t, err)
    56  
    57  	_, err = os.Create(filepath.Join(subdir, "bcd1.so"))
    58  	assert.NoError(t, err)
    59  
    60  	fs, err := ListAllSharedLibFiles(dir)
    61  	assert.NoError(t, err)
    62  	assert.Len(t, fs, 1)
    63  	assert.Equal(t, filepath.Join(dir, "bcd.so"), fs[0])
    64  }