File: cp_test.go

package info (click to toggle)
golang-github-u-root-uio 0.0~git20240224.d2acac8-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 276 kB
  • sloc: sh: 25; makefile: 2
file content (137 lines) | stat: -rw-r--r-- 3,486 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2019 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package cp

import (
	"errors"
	"fmt"
	"io/fs"
	"os"
	"path/filepath"
	"strings"
	"testing"

	"golang.org/x/sys/unix"
)

var (
	testdata = []byte("This is a test string")
)

func TestCopySimple(t *testing.T) {
	var err error
	tmpdirDst := t.TempDir()
	tmpdirSrc := t.TempDir()

	srcfiles := make([]*os.File, 2)
	dstfiles := make([]*os.File, 2)
	for iterator := range srcfiles {
		srcfiles[iterator], err = os.CreateTemp(tmpdirSrc, "file-to-copy"+fmt.Sprintf("%d", iterator))
		if err != nil {
			t.Errorf("failed to create temp file: %q", err)
		}
		if _, err = srcfiles[iterator].Write(testdata); err != nil {
			t.Errorf("failed to write testdata to file")
		}
		dstfiles[iterator], err = os.CreateTemp(tmpdirDst, "file-to-copy"+fmt.Sprintf("%d", iterator))
		if err != nil {
			t.Errorf("failed to create temp file: %q", err)
		}
	}

	sl := filepath.Join(tmpdirDst, "test-symlink")
	if err := os.Symlink(srcfiles[1].Name(), sl); err != nil {
		t.Errorf("creating symlink failed")
	}

	for _, tt := range []struct {
		name    string
		srcfile string
		dstfile string
		opt     Options
		wantErr error
	}{
		{
			name:    "Success",
			srcfile: srcfiles[0].Name(),
			dstfile: dstfiles[0].Name(),
			opt:     Default,
		},
		{
			name:    "SrcDstDirctoriesSuccess",
			srcfile: tmpdirSrc,
			dstfile: tmpdirDst,
		},
		{
			name:    "SrcNotExist",
			srcfile: "file-does-not-exist",
			dstfile: dstfiles[0].Name(),
			wantErr: fs.ErrNotExist,
		},
		{
			name:    "DstIsDirectory",
			srcfile: srcfiles[0].Name(),
			dstfile: tmpdirDst,
			wantErr: unix.EISDIR,
		},
		{
			name:    "CopySymlink",
			srcfile: sl,
			dstfile: dstfiles[1].Name(),
			opt: Options{
				NoFollowSymlinks: false,
			},
		},
		{
			name:    "CopySymlinkFollow",
			srcfile: sl,
			dstfile: filepath.Join(tmpdirDst, "followed-symlink"),
			opt: Options{
				NoFollowSymlinks: true,
			},
		},
	} {
		t.Run(tt.name, func(t *testing.T) {
			err := Copy(tt.srcfile, tt.dstfile)
			if !errors.Is(err, tt.wantErr) {
				t.Errorf("Test %q failed. Want: %q, Got: %q", tt.name, tt.wantErr, err)
			}
		})
		// After every test with NoFollowSymlink we have to delete the created symlink.
		if strings.Contains(tt.dstfile, "symlink") {
			os.Remove(tt.dstfile)
		}

		t.Run(tt.name, func(t *testing.T) {
			if err := tt.opt.Copy(tt.srcfile, tt.dstfile); !errors.Is(err, tt.wantErr) {
				t.Errorf("%q failed. Want: %q, Got: %q", tt.name, tt.wantErr, err)
			}
		})
		// After every test with NoFollowSymlink we have to delete the created symlink.
		if strings.Contains(tt.dstfile, "symlink") {
			os.Remove(tt.dstfile)
		}

		t.Run(tt.name, func(t *testing.T) {
			if err := tt.opt.CopyTree(tt.srcfile, tt.dstfile); !errors.Is(err, tt.wantErr) {
				t.Errorf("Test %q failed. Want: %q, Got: %q", tt.name, tt.wantErr, err)
			}
		})
		// After every test with NoFollowSymlink we have to delete the created symlink.
		if strings.Contains(tt.dstfile, "symlink") {
			os.Remove(tt.dstfile)
		}

		t.Run(tt.name, func(t *testing.T) {
			if err := CopyTree(tt.srcfile, tt.dstfile); !errors.Is(err, tt.wantErr) {
				t.Errorf("Test %q failed. Want: %q, Got: %q", tt.name, tt.wantErr, err)
			}
		})
		// After every test with NoFollowSymlink we have to delete the created symlink.
		if strings.Contains(tt.dstfile, "symlink") {
			os.Remove(tt.dstfile)
		}
	}
}