github.com/kaydxh/golang@v0.0.131/go/io/copy_test.go (about)

     1  /*
     2  MIT License
     3  
     4  Copyright (c) 2020 kay
     5  
     6  Permission is hereby granted, free of charge, to any person obtaining a copy
     7  of this software and associated documentation files (the "Software"), to deal
     8  in the Software without restriction, including without limitation the rights
     9  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    10  copies of the Software, and to permit persons to whom the Software is
    11  furnished to do so, subject to the following conditions:
    12  
    13  The above copyright notice and this permission notice shall be included in all
    14  copies or substantial portions of the Software.
    15  
    16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    21  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    22  SOFTWARE.
    23  */
    24  
    25  package io_test
    26  
    27  import (
    28  	"bytes"
    29  	"fmt"
    30  	"io/ioutil"
    31  	"math/rand"
    32  	"os"
    33  	"path/filepath"
    34  	"testing"
    35  
    36  	"github.com/kaydxh/golang/go/io"
    37  	io_ "github.com/kaydxh/golang/go/io"
    38  	"gotest.tools/v3/assert"
    39  )
    40  
    41  func TestCopyDir(t *testing.T) {
    42  	srcDir, err := ioutil.TempDir("", "srcDir")
    43  	if err != nil {
    44  		t.Errorf("expect nil, got %v", err)
    45  	}
    46  	populateSrcDir(t, srcDir, 3)
    47  
    48  	dstDir, err := ioutil.TempDir("", "dstDir")
    49  	if err != nil {
    50  		t.Errorf("expect nil, got %v", err)
    51  	}
    52  	defer os.RemoveAll(dstDir)
    53  
    54  	err = io.CopyDir(srcDir, dstDir, io.Content)
    55  	if err != nil {
    56  		t.Errorf("expect nil, got %v", err)
    57  	}
    58  
    59  	err = filepath.Walk(srcDir, func(srcPath string, f os.FileInfo, err error) error {
    60  		if err != nil {
    61  			return err
    62  		}
    63  
    64  		// Rebase path
    65  		relPath, err := filepath.Rel(srcDir, srcPath)
    66  		assert.NilError(t, err)
    67  		if relPath == "." {
    68  			return nil
    69  		}
    70  
    71  		dstPath := filepath.Join(dstDir, relPath)
    72  		// If we add non-regular dirs and files to the test
    73  		// then we need to add more checks here.
    74  		dstFileInfo, err := os.Lstat(dstPath)
    75  		assert.NilError(t, err)
    76  
    77  		fmt.Println("dstFileInfo: ", dstFileInfo)
    78  
    79  		return nil
    80  	})
    81  
    82  	if err != nil {
    83  		t.Errorf("expect nil, got %v", err)
    84  	}
    85  
    86  }
    87  
    88  func TestCopyFile(t *testing.T) {
    89  	dir, err := ioutil.TempDir("", "file-copy-check")
    90  	if err != nil {
    91  		t.Errorf("expect nil, got %v", err)
    92  	}
    93  	defer os.RemoveAll(dir)
    94  
    95  	srcFilename := filepath.Join(dir, "srcFilename")
    96  	dstFilename := filepath.Join(dir, "dstilename")
    97  	fmt.Println("srcFilename: , dstFilename: ", srcFilename, dstFilename)
    98  
    99  	buf := []byte("hello world")
   100  	err = ioutil.WriteFile(srcFilename, buf, 0777)
   101  	if err != nil {
   102  		t.Errorf("expect nil, got %v", err)
   103  	}
   104  
   105  	err = io.CopyFile(srcFilename, dstFilename)
   106  	if err != nil {
   107  		t.Errorf("expect nil, got %v", err)
   108  	}
   109  
   110  	readBuf, err := ioutil.ReadFile(srcFilename)
   111  	if err != nil {
   112  		t.Errorf("expect nil, got %v", err)
   113  	}
   114  	fmt.Println("srcFilename content: ", string(readBuf))
   115  
   116  	readBuf, err = ioutil.ReadFile(dstFilename)
   117  	if err != nil {
   118  		t.Errorf("expect nil, got %v", err)
   119  	}
   120  	fmt.Println("dstFilename content: ", string(readBuf))
   121  
   122  	if !bytes.Equal(buf, readBuf) {
   123  		t.Errorf("expect true, got %v", false)
   124  	}
   125  }
   126  
   127  func TestCopyAll(t *testing.T) {
   128  	testCases := []struct {
   129  		src string
   130  		dst string
   131  	}{
   132  		{
   133  			src: "./testdata/file/1.txt",
   134  			dst: "./testdata.copy/file",
   135  		},
   136  		/*
   137  			{
   138  				src: "./testdata/dir",
   139  				dst: "./testdata.copy/dir",
   140  			},
   141  		*/
   142  	}
   143  	for i, testCase := range testCases {
   144  		t.Run(fmt.Sprintf("test-%d", i), func(t *testing.T) {
   145  			err := io_.CopyAll(testCase.src, testCase.dst)
   146  			if err != nil {
   147  				t.Fatalf("copy all expected nil, got %v", err)
   148  			}
   149  		})
   150  	}
   151  
   152  }
   153  
   154  func randomMode(baseMode int) os.FileMode {
   155  	for i := 0; i < 7; i++ {
   156  		baseMode = baseMode | (1&rand.Intn(2))<<uint(i)
   157  	}
   158  	return os.FileMode(baseMode)
   159  }
   160  
   161  func populateSrcDir(t *testing.T, srcDir string, remainingDepth int) {
   162  	if remainingDepth == 0 {
   163  		return
   164  	}
   165  
   166  	for i := 0; i < 10; i++ {
   167  		dirName := filepath.Join(srcDir, fmt.Sprintf("srcdir-%d", i))
   168  		// Owner all bits set
   169  		assert.NilError(t, os.Mkdir(dirName, randomMode(0700)))
   170  		populateSrcDir(t, dirName, remainingDepth-1)
   171  	}
   172  
   173  	for i := 0; i < 10; i++ {
   174  		fileName := filepath.Join(srcDir, fmt.Sprintf("srcfile-%d", i))
   175  		// Owner read bit set
   176  		assert.NilError(t, ioutil.WriteFile(fileName, []byte{}, randomMode(0400)))
   177  	}
   178  
   179  }