1
1
/*
2
2
MIT License
3
3
4
- Copyright (c) 2023 Rick
4
+ Copyright (c) 2023-2024 Rick
5
5
6
6
Permission is hereby granted, free of charge, to any person obtaining a copy
7
7
of this software and associated documentation files (the "Software"), to deal
@@ -40,34 +40,35 @@ type FakeExecer struct {
40
40
ExpectOS string
41
41
ExpectArch string
42
42
ExpectLookPath string
43
+ Env map [string ]string
43
44
}
44
45
45
- func (f FakeExecer ) WithContext (ctx context.Context ) Execer {
46
+ func (f * FakeExecer ) WithContext (ctx context.Context ) Execer {
46
47
return f
47
48
}
48
49
49
50
// LookPath is a fake method
50
- func (f FakeExecer ) LookPath (path string ) (string , error ) {
51
+ func (f * FakeExecer ) LookPath (path string ) (string , error ) {
51
52
return f .ExpectLookPath , f .ExpectLookPathError
52
53
}
53
54
54
55
// Command is a fake method
55
- func (f FakeExecer ) Command (name string , arg ... string ) ([]byte , error ) {
56
+ func (f * FakeExecer ) Command (name string , arg ... string ) ([]byte , error ) {
56
57
return []byte (f .ExpectOutput ), f .ExpectError
57
58
}
58
59
59
60
// RunCommand runs a command
60
- func (f FakeExecer ) RunCommand (name string , arg ... string ) error {
61
+ func (f * FakeExecer ) RunCommand (name string , arg ... string ) error {
61
62
return f .ExpectError
62
63
}
63
64
64
65
// RunCommandInDir is a fake method
65
- func (f FakeExecer ) RunCommandInDir (name , dir string , args ... string ) error {
66
+ func (f * FakeExecer ) RunCommandInDir (name , dir string , args ... string ) error {
66
67
return f .ExpectError
67
68
}
68
69
69
70
// RunCommandAndReturn is a fake method
70
- func (f FakeExecer ) RunCommandAndReturn (name , dir string , args ... string ) (result string , err error ) {
71
+ func (f * FakeExecer ) RunCommandAndReturn (name , dir string , args ... string ) (result string , err error ) {
71
72
if err = f .ExpectError ; err == nil {
72
73
result = f .ExpectOutput
73
74
} else {
@@ -78,41 +79,57 @@ func (f FakeExecer) RunCommandAndReturn(name, dir string, args ...string) (resul
78
79
}
79
80
80
81
// RunCommandWithSudo is a fake method
81
- func (f FakeExecer ) RunCommandWithSudo (name string , args ... string ) (err error ) {
82
+ func (f * FakeExecer ) RunCommandWithSudo (name string , args ... string ) (err error ) {
82
83
return f .ExpectError
83
84
}
84
85
85
86
// RunCommandWithBuffer is a fake method
86
- func (f FakeExecer ) RunCommandWithBuffer (name , dir string , stdout , stderr * bytes.Buffer , args ... string ) error {
87
+ func (f * FakeExecer ) RunCommandWithBuffer (name , dir string , stdout , stderr * bytes.Buffer , args ... string ) error {
87
88
return f .ExpectError
88
89
}
89
90
90
91
// RunCommandWithIO is a fake method
91
- func (f FakeExecer ) RunCommandWithIO (name , dir string , stdout , stderr io.Writer , p chan Process , args ... string ) error {
92
+ func (f * FakeExecer ) RunCommandWithIO (name , dir string , stdout , stderr io.Writer , p chan Process , args ... string ) error {
92
93
return f .ExpectError
93
94
}
94
95
95
96
// RunCommandWithEnv is a fake method
96
- func (f FakeExecer ) RunCommandWithEnv (name string , argv , envv []string , stdout , stderr io.Writer ) error {
97
+ func (f * FakeExecer ) RunCommandWithEnv (name string , argv , envv []string , stdout , stderr io.Writer ) error {
97
98
return f .ExpectError
98
99
}
99
100
100
101
// SystemCall is a fake method
101
- func (f FakeExecer ) SystemCall (name string , argv []string , envv []string ) error {
102
+ func (f * FakeExecer ) SystemCall (name string , argv []string , envv []string ) error {
102
103
return f .ExpectError
103
104
}
104
105
105
106
// MkdirAll is the wrapper of os.MkdirAll
106
- func (f FakeExecer ) MkdirAll (path string , perm os.FileMode ) error {
107
+ func (f * FakeExecer ) MkdirAll (path string , perm os.FileMode ) error {
107
108
return f .ExpectError
108
109
}
109
110
110
111
// OS returns the os name
111
- func (f FakeExecer ) OS () string {
112
+ func (f * FakeExecer ) OS () string {
112
113
return f .ExpectOS
113
114
}
114
115
115
116
// Arch returns the os arch
116
- func (f FakeExecer ) Arch () string {
117
+ func (f * FakeExecer ) Arch () string {
117
118
return f .ExpectArch
118
119
}
120
+
121
+ // Getenv returns the env value by key
122
+ func (f * FakeExecer ) Getenv (key string ) string {
123
+ return f .Env [key ]
124
+ }
125
+
126
+ // Setenv sets the key-value pair into the env
127
+ func (f * FakeExecer ) Setenv (key , value string ) error {
128
+ if f .Env == nil {
129
+ f .Env = make (map [string ]string )
130
+ }
131
+ f .Env [key ] = value
132
+ return nil
133
+ }
134
+
135
+ var _ Execer = & FakeExecer {}
0 commit comments