github.com/Schaudge/grailbase@v0.0.0-20240223061707-44c758a471c0/file/localfile_test.go (about) 1 // Copyright 2018 GRAIL, Inc. All rights reserved. 2 // Use of this source code is governed by the Apache-2.0 3 // license that can be found in the LICENSE file. 4 5 package file_test 6 7 import ( 8 "context" 9 "fmt" 10 "io/ioutil" 11 "os" 12 "path/filepath" 13 "testing" 14 15 "github.com/Schaudge/grailbase/file" 16 filetestutil "github.com/Schaudge/grailbase/file/internal/testutil" 17 "github.com/grailbio/testutil" 18 "github.com/grailbio/testutil/assert" 19 "github.com/stretchr/testify/require" 20 ) 21 22 func TestAll(t *testing.T) { 23 tempDir, cleanup := testutil.TempDir(t, "", "") 24 defer cleanup() 25 impl := file.NewLocalImplementation() 26 ctx := context.Background() 27 filetestutil.TestStandard(ctx, t, impl, tempDir) 28 } 29 30 func TestEmptyPath(t *testing.T) { 31 _, err := file.Create(context.Background(), "") 32 require.Regexp(t, "empty pathname", err) 33 } 34 35 // Test that Create on a symlink will preserve it. 36 func TestCreateSymlink(t *testing.T) { 37 dir0, cleanup0 := testutil.TempDir(t, "", "") 38 dir1, cleanup1 := testutil.TempDir(t, "", "") 39 defer cleanup1() 40 defer cleanup0() 41 42 newPath := filepath.Join(dir1, "new") 43 oldPath := filepath.Join(dir0, "old") 44 require.NoError(t, os.Symlink(oldPath, newPath)) 45 require.NoError(t, ioutil.WriteFile(oldPath, []byte("hoofah"), 0777)) 46 47 ctx := context.Background() 48 w, err := file.Create(context.Background(), newPath) 49 require.NoError(t, err) 50 _, err = w.Writer(ctx).Write([]byte("hello")) 51 require.NoError(t, err) 52 require.NoError(t, w.Close(ctx)) 53 54 data, err := ioutil.ReadFile(newPath) 55 require.NoError(t, err) 56 require.Equal(t, "hello", string(data)) 57 58 // The file should have been created in the symlink dest dir. 59 data, err = ioutil.ReadFile(oldPath) 60 require.NoError(t, err) 61 require.Equal(t, "hello", string(data)) 62 } 63 64 func TestCreateDirectory(t *testing.T) { 65 tmp, cleanup0 := testutil.TempDir(t, "", "") 66 defer cleanup0() 67 68 dirPath := file.Join(tmp, "dir") 69 err := os.Mkdir(dirPath, 0777) 70 assert.Nil(t, err) 71 72 ctx := context.Background() 73 _, err = file.Create(ctx, dirPath) 74 require.EqualError(t, err, fmt.Sprintf("file.Create %s: is a directory", dirPath)) 75 }