github.com/Schaudge/grailbase@v0.0.0-20240223061707-44c758a471c0/cmd/grail-file/main_test.go (about)

     1  package main
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"os"
     7  	"sort"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/Schaudge/grailbase/cmd/grail-file/cmd"
    12  	"github.com/Schaudge/grailbase/file"
    13  	"github.com/grailbio/testutil"
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func readFile(path string) string {
    18  	got, err := file.ReadFile(context.Background(), path)
    19  	if err != nil {
    20  		return err.Error()
    21  	}
    22  	return string(got)
    23  }
    24  
    25  func TestLs(t *testing.T) {
    26  	doLs := func(args ...string) []string {
    27  		out := bytes.Buffer{}
    28  		assert.NoError(t, cmd.Ls(context.Background(), &out, args))
    29  		s := strings.Split(strings.TrimSpace(out.String()), "\n")
    30  		sort.Strings(s)
    31  		return s
    32  	}
    33  
    34  	ctx := context.Background()
    35  	tmpDir, cleanup := testutil.TempDir(t, "", "")
    36  	defer cleanup()
    37  
    38  	path0 := file.Join(tmpDir, "0.txt")
    39  	path1 := file.Join(tmpDir, "d/1.txt")
    40  	assert.NoError(t, file.WriteFile(ctx, path0, []byte("0")))
    41  	assert.NoError(t, file.WriteFile(ctx, path1, []byte("1")))
    42  	assert.Equal(t,
    43  		[]string{tmpDir + "/0.txt", tmpDir + "/d/"},
    44  		doLs(tmpDir))
    45  	assert.Equal(t,
    46  		[]string{tmpDir + "/0.txt", tmpDir + "/d/1.txt"},
    47  		doLs("-R", tmpDir))
    48  
    49  	s := doLs("-l", tmpDir)
    50  	assert.Equal(t, 2, len(s))
    51  	assert.Regexp(t, tmpDir+"/0.txt\t1\t20.*", s[0])
    52  	assert.Equal(t, tmpDir+"/d/", s[1])
    53  
    54  	s = doLs("-l", "-R", tmpDir)
    55  	assert.Equal(t, 2, len(s))
    56  	assert.Regexp(t, tmpDir+"/0.txt\t1\t20.*", s[0])
    57  	assert.Regexp(t, tmpDir+"/d/1.txt\t1\t20.*", s[1])
    58  }
    59  
    60  func TestCp(t *testing.T) {
    61  	ctx := context.Background()
    62  
    63  	tmpDir, cleanup := testutil.TempDir(t, "", "")
    64  	defer cleanup()
    65  	src0Path := file.Join(tmpDir, "tmp0.txt")
    66  	src1Path := file.Join(tmpDir, "tmp1.txt")
    67  	expected0 := "tmp0"
    68  	expected1 := "tmp1"
    69  	assert.NoError(t, file.WriteFile(ctx, src0Path, []byte(expected0)))
    70  	assert.NoError(t, file.WriteFile(ctx, src1Path, []byte(expected1)))
    71  
    72  	// "cp xxx yyy", where yyy doesn't exist.
    73  	dstPath := file.Join(tmpDir, "d0.txt")
    74  	assert.NoError(t, cmd.Cp(ctx, os.Stdout, []string{src0Path, dstPath}))
    75  	assert.Equal(t, expected0, readFile(dstPath))
    76  
    77  	// "cp x0 x1 yyy", where yyy doesn't exist
    78  	dstPath = file.Join(tmpDir, "d1")
    79  	assert.NoError(t, cmd.Cp(ctx, os.Stdout, []string{src0Path, src1Path, dstPath}))
    80  	assert.Equal(t, expected0, readFile(file.Join(dstPath, "tmp0.txt")))
    81  	assert.Equal(t, expected1, readFile(file.Join(dstPath, "tmp1.txt")))
    82  
    83  	// Try "cp xxx yyy/", where yyy doesn't exist. Cp should create file yyy/xxx.
    84  	dstDir := file.Join(tmpDir, "testdir0")
    85  	assert.NoError(t, cmd.Cp(ctx, os.Stdout, []string{src0Path, dstDir + "/"}))
    86  	assert.Equal(t, expected0, readFile(file.Join(dstDir, "tmp0.txt")))
    87  
    88  	dstDir = tmpDir + "/d2"
    89  	assert.NoError(t, os.Mkdir(dstDir, 0700))
    90  	assert.NoError(t, cmd.Cp(ctx, os.Stdout, []string{src0Path, dstDir}))
    91  	assert.Equal(t, expected0, readFile(file.Join(dstDir, "tmp0.txt")))
    92  }
    93  
    94  func TestCpRecursive(t *testing.T) {
    95  	ctx := context.Background()
    96  	tmpDir, cleanup := testutil.TempDir(t, "", "")
    97  	defer cleanup()
    98  
    99  	srcDir := file.Join(tmpDir, "dir")
   100  	path0 := "/dir/tmp0.txt"
   101  	path1 := "/dir/dir2/tmp1.txt"
   102  	path2 := "/dir/dir2/dir3/tmp2.txt"
   103  	expected0 := "tmp0"
   104  	expected1 := "tmp1"
   105  	expected2 := "tmp2"
   106  	assert.NoError(t, file.WriteFile(ctx, srcDir+path0, []byte(expected0)))
   107  	assert.NoError(t, file.WriteFile(ctx, srcDir+path1, []byte(expected1)))
   108  	assert.NoError(t, file.WriteFile(ctx, srcDir+path2, []byte(expected2)))
   109  	dstDir := file.Join(tmpDir, "dir1")
   110  	assert.NoError(t, cmd.Cp(ctx, os.Stdout, []string{"-R", srcDir, dstDir}))
   111  	assert.Equal(t, expected0, readFile(dstDir+path0))
   112  	assert.Equal(t, expected1, readFile(dstDir+path1))
   113  	assert.Equal(t, expected2, readFile(dstDir+path2))
   114  }
   115  
   116  func TestRm(t *testing.T) {
   117  	ctx := context.Background()
   118  	tmpDir, cleanup := testutil.TempDir(t, "", "")
   119  	defer cleanup()
   120  	src0Path := file.Join(tmpDir, "tmp0.txt")
   121  	src1Path := file.Join(tmpDir, "tmp1.txt")
   122  	src2Path := file.Join(tmpDir, "tmp2.txt")
   123  	assert.NoError(t, file.WriteFile(ctx, src0Path, []byte("0")))
   124  	assert.NoError(t, file.WriteFile(ctx, src1Path, []byte("1")))
   125  	assert.NoError(t, file.WriteFile(ctx, src2Path, []byte("2")))
   126  
   127  	assert.Equal(t, "0", readFile(src0Path))
   128  	assert.Equal(t, "1", readFile(src1Path))
   129  	assert.Equal(t, "2", readFile(src2Path))
   130  
   131  	assert.NoError(t, cmd.Rm(ctx, os.Stdout, []string{src0Path, src1Path}))
   132  	assert.Regexp(t, "no such file", readFile(src0Path))
   133  	assert.Regexp(t, "no such file", readFile(src1Path))
   134  	assert.Equal(t, "2", readFile(src2Path))
   135  
   136  	assert.NoError(t, cmd.Rm(ctx, os.Stdout, []string{src2Path}))
   137  	assert.Regexp(t, "no such file", readFile(src0Path))
   138  	assert.Regexp(t, "no such file", readFile(src1Path))
   139  	assert.Regexp(t, "no such file", readFile(src2Path))
   140  }
   141  
   142  func TestRmRecursive(t *testing.T) {
   143  	ctx := context.Background()
   144  	tmpDir, cleanup := testutil.TempDir(t, "", "")
   145  	defer cleanup()
   146  	src0Path := file.Join(tmpDir, "dir/tmp0.txt")
   147  	src1Path := file.Join(tmpDir, "dir/dir2/tmp1.txt")
   148  	src2Path := file.Join(tmpDir, "dir/dir2/dir3/tmp2.txt")
   149  	assert.NoError(t, file.WriteFile(ctx, src0Path, []byte("0")))
   150  	assert.NoError(t, file.WriteFile(ctx, src1Path, []byte("1")))
   151  	assert.NoError(t, file.WriteFile(ctx, src2Path, []byte("2")))
   152  
   153  	assert.NoError(t, cmd.Rm(ctx, os.Stdout, []string{"-R", file.Join(tmpDir, "dir/dir2")}))
   154  	assert.Equal(t, "0", readFile(src0Path))
   155  	assert.Regexp(t, "no such file", readFile(src1Path))
   156  	assert.Regexp(t, "no such file", readFile(src2Path))
   157  }