crossplane

Форк
0
/
find_test.go 
118 строк · 2.7 Кб
1
/*
2
Copyright 2020 The Crossplane Authors.
3

4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
7

8
	http://www.apache.org/licenses/LICENSE-2.0
9

10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
15
*/
16

17
package xpkg
18

19
import (
20
	"os"
21
	"testing"
22

23
	"github.com/google/go-cmp/cmp"
24
	"github.com/spf13/afero"
25

26
	"github.com/crossplane/crossplane-runtime/pkg/errors"
27
	"github.com/crossplane/crossplane-runtime/pkg/test"
28
)
29

30
func TestFindXpkgInDir(t *testing.T) {
31
	match := afero.NewMemMapFs()
32
	_ = afero.WriteFile(match, "one.xpkg", []byte{}, StreamFileMode)
33

34
	multi := afero.NewMemMapFs()
35
	_ = afero.WriteFile(multi, "one.xpkg", []byte{}, StreamFileMode)
36
	_ = afero.WriteFile(multi, "two.xpkg", []byte{}, StreamFileMode)
37

38
	type args struct {
39
		root string
40
		fs   afero.Fs
41
	}
42

43
	type want struct {
44
		path string
45
		err  error
46
	}
47

48
	cases := map[string]struct {
49
		reason string
50
		args   args
51
		want   want
52
	}{
53
		"NoMatch": {
54
			reason: "We should return an error if no matches.",
55
			args: args{
56
				root: ".",
57
				fs:   afero.NewMemMapFs(),
58
			},
59
			want: want{
60
				err: errors.New(errNoMatch),
61
			},
62
		},
63
		"MultiMatch": {
64
			reason: "We should return an error if multiple matches.",
65
			args: args{
66
				root: ".",
67
				fs:   multi,
68
			},
69
			want: want{
70
				err: errors.New(errMultiMatch),
71
			},
72
		},
73
		"NotExist": {
74
			reason: "We should return an error root does not exist.",
75
			args: args{
76
				root: "/test",
77
				fs:   afero.NewMemMapFs(),
78
			},
79
			want: want{
80
				err: &os.PathError{Op: "open", Path: "/test", Err: os.ErrNotExist},
81
			},
82
		},
83
		"NotDir": {
84
			reason: "We should return an error if root is not a directory.",
85
			args: args{
86
				root: "one.xpkg",
87
				fs:   multi,
88
			},
89
			want: want{
90
				err: &os.PathError{Op: "readdir", Path: "one.xpkg", Err: errors.New("not a dir")},
91
			},
92
		},
93
		"Successful": {
94
			reason: "We should return file path if one package exists.",
95
			args: args{
96
				root: ".",
97
				fs:   match,
98
			},
99
			want: want{
100
				path: "one.xpkg",
101
			},
102
		},
103
	}
104

105
	for name, tc := range cases {
106
		t.Run(name, func(t *testing.T) {
107
			path, err := FindXpkgInDir(tc.args.fs, tc.args.root)
108

109
			if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" {
110
				t.Errorf("\n%s\nFindXpkgInDir(...): -want, +got:\n%s", tc.reason, diff)
111
			}
112

113
			if diff := cmp.Diff(tc.want.path, path); diff != "" {
114
				t.Errorf("\n%s\nFindXpkgInDir(...): -want, +got:\n%s", tc.reason, diff)
115
			}
116
		})
117
	}
118
}
119

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.