aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/osutil/osutil_test.go
blob: 713a62c14ce7539d0bdd5bafa3d105e5f6a72eee (plain)
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
// Copyright 2017 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.

package osutil

import (
	"fmt"
	"os"
	"path/filepath"
	"strings"
	"testing"
)

func TestIsExist(t *testing.T) {
	if f := os.Args[0]; !IsExist(f) {
		t.Fatalf("executable %v does not exist", f)
	}
	if f := os.Args[0] + "-foo-bar-buz"; IsExist(f) {
		t.Fatalf("file %v exists", f)
	}
}

func TestCopyFiles(t *testing.T) {
	type Test struct {
		files    []string
		patterns map[string]bool
		err      string
	}
	tests := []Test{
		{
			files: []string{
				"foo",
				"bar",
				"baz/foo",
				"baz/bar",
			},
			patterns: map[string]bool{
				"foo":     true,
				"bar":     false,
				"qux":     false,
				"baz/foo": true,
				"baz/bar": false,
			},
		},
		{
			files: []string{
				"foo",
			},
			patterns: map[string]bool{
				"bar": true,
			},
			err: "file bar does not exist",
		},
		{
			files: []string{
				"baz/foo",
				"baz/bar",
			},
			patterns: map[string]bool{
				"baz/*": true,
			},
		},
		{
			files: []string{
				"qux/foo/foo",
				"qux/foo/bar",
				"qux/bar/foo",
				"qux/bar/bar",
			},
			patterns: map[string]bool{
				"qux/*/*": false,
			},
		},
	}
	for _, link := range []bool{false, true} {
		fn, fnName := CopyFiles, "CopyFiles"
		if link {
			fn, fnName = LinkFiles, "LinkFiles"
		}
		t.Run(fnName, func(t *testing.T) {
			for i, test := range tests {
				t.Run(fmt.Sprint(i), func(t *testing.T) {
					dir := t.TempDir()
					src := filepath.Join(dir, "src")
					dst := filepath.Join(dir, "dst")
					for _, file := range test.files {
						file = filepath.Join(src, filepath.FromSlash(file))
						if err := MkdirAll(filepath.Dir(file)); err != nil {
							t.Fatal(err)
						}
						if err := WriteFile(file, []byte{'a'}); err != nil {
							t.Fatal(err)
						}
					}
					if err := fn(src, dst, test.patterns); err != nil {
						if test.err != "" {
							if strings.Contains(err.Error(), test.err) {
								return
							}
							t.Fatalf("got err %q, want %q", err, test.err)
						}
						t.Fatal(err)
					} else if test.err != "" {
						t.Fatalf("got no err, want %q", test.err)
					}
					if err := os.RemoveAll(src); err != nil {
						t.Fatal(err)
					}
					for _, file := range test.files {
						if !IsExist(filepath.Join(dst, filepath.FromSlash(file))) {
							t.Fatalf("%v does not exist in dst", file)
						}
					}
					if !FilesExist(dst, test.patterns) {
						t.Fatalf("dst files don't exist after copy")
					}
				})
			}
		})
	}
}