github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/pkg/utils/file_test.go (about) 1 // Copyright 2020 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package utils 15 16 import ( 17 "os" 18 "path/filepath" 19 "testing" 20 21 "github.com/pingcap/tiflow/dm/pkg/terror" 22 "github.com/stretchr/testify/require" 23 ) 24 25 func TestFile(t *testing.T) { 26 // dir not exists 27 require.False(t, IsFileExists("invalid-path")) 28 require.False(t, IsDirExists("invalid-path")) 29 size, err := GetFileSize("invalid-path") 30 require.True(t, terror.ErrGetFileSize.Equal(err)) 31 require.Equal(t, int64(0), size) 32 33 // dir exists 34 d := t.TempDir() 35 require.False(t, IsFileExists(d)) 36 require.True(t, IsDirExists(d)) 37 size, err = GetFileSize(d) 38 require.True(t, terror.ErrGetFileSize.Equal(err)) 39 require.Equal(t, int64(0), size) 40 41 // file not exists 42 f := filepath.Join(d, "text-file") 43 require.False(t, IsFileExists(f)) 44 require.False(t, IsDirExists(f)) 45 size, err = GetFileSize(f) 46 require.True(t, terror.ErrGetFileSize.Equal(err)) 47 require.Equal(t, int64(0), size) 48 49 // create a file 50 require.NoError(t, os.WriteFile(f, []byte("some content"), 0o644)) 51 require.True(t, IsFileExists(f)) 52 require.False(t, IsDirExists(f)) 53 size, err = GetFileSize(f) 54 require.NoError(t, err) 55 require.Equal(t, int64(len("some content")), size) 56 }