github.com/saymoon/flop@v0.1.6-0.20201205092451-00912199cc96/copy_win_test.go (about)

     1  // +build windows
     2  
     3  package flop
     4  
     5  import (
     6  	"github.com/stretchr/testify/assert"
     7  	"io/ioutil"
     8  	"os"
     9  	"testing"
    10  )
    11  
    12  func TestForPresenceOfFileCopyErrors(t *testing.T) {
    13  	assert := assert.New(t)
    14  	tests := []struct {
    15  		name                 string
    16  		copyFunc             func(string, os.FileInfo, string, os.FileInfo) error
    17  		inFile               string
    18  		outFile              string
    19  		errSubstringExpected string
    20  	}{
    21  		{
    22  			name:                 "src_path_which_cannot_be_opened",
    23  			inFile:               "////",
    24  			outFile:              tmpFile(),
    25  			errSubstringExpected: ErrCannotStatFile.Error(),
    26  		},
    27  	}
    28  	for _, tt := range tests {
    29  		t.Run(tt.name, func(t *testing.T) {
    30  			err := SimpleCopy(tt.inFile, tt.outFile)
    31  			assert.True(errContains(err, tt.errSubstringExpected))
    32  		})
    33  	}
    34  }
    35  
    36  func TestFileCopyOnDstWithInvalidPermissionsReturnsAccessError(t *testing.T) {
    37  	assert := assert.New(t)
    38  	tests := []struct {
    39  		name         string
    40  		opts         Options
    41  		errSubstring string
    42  	}{
    43  		{"atomic", Options{Atomic: true}, ErrCannotRenameTempFile.Error()},
    44  		{"atomic", Options{Atomic: false}, ErrCannotOpenOrCreateDstFile.Error()},
    45  	}
    46  	for _, tt := range tests {
    47  		t.Run(tt.name, func(t *testing.T) {
    48  			// create and write to source inFile
    49  			src := tmpFile()
    50  			content := []byte("foo")
    51  			assert.Nil(ioutil.WriteFile(src, content, 0644))
    52  
    53  			dst := tmpFile()
    54  			// explicitly set our dst inFile perms so that we cannot copy
    55  			assert.Nil(os.Chmod(dst, 0111))
    56  
    57  			err := Copy(src, dst, tt.opts)
    58  			assert.True(errContains(err, tt.errSubstring), "err is: %s", err)
    59  
    60  			// change perms back to ensure we can read to verify content
    61  			assert.Nil(os.Chmod(dst, 0655))
    62  		})
    63  	}
    64  }