Skip to content

Commit 9865dbb

Browse files
authored
Merge pull request #9 from LinuxSuRen/feat/env
feat: support to get and set env
2 parents a36148d + 605d8e1 commit 9865dbb

File tree

7 files changed

+58
-21
lines changed

7 files changed

+58
-21
lines changed

.github/workflows/build.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
- name: Set up Go
1212
uses: actions/setup-go@v3
1313
with:
14-
go-version: 1.18.x
14+
go-version: 1.23.x
1515
- uses: actions/[email protected]
1616
- name: Test
1717
run: |

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
coverage.out
2+
.idea/

command.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
MIT License
33
4-
Copyright (c) 2023 Rick
4+
Copyright (c) 2023-2024 Rick
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy
77
of this software and associated documentation files (the "Software"), to deal
@@ -50,6 +50,8 @@ type Execer interface {
5050
MkdirAll(path string, perm os.FileMode) error
5151
OS() string
5252
Arch() string
53+
Getenv(string) string
54+
Setenv(string, string) error
5355
WithContext(context.Context) Execer
5456
}
5557

@@ -184,6 +186,16 @@ func (e *defaultExecer) Arch() string {
184186
return runtime.GOARCH
185187
}
186188

189+
// Getenv returns the env value by key
190+
func (e *defaultExecer) Getenv(key string) string {
191+
return os.Getenv(key)
192+
}
193+
194+
// Setenv sets the key-value pair into the env
195+
func (e *defaultExecer) Setenv(key, value string) error {
196+
return os.Setenv(key, value)
197+
}
198+
187199
// RunCommandAndReturn runs a command, then returns the output
188200
func (e *defaultExecer) RunCommandAndReturn(name, dir string, args ...string) (result string, err error) {
189201
stdout := &bytes.Buffer{}

command_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
MIT License
33
4-
Copyright (c) 2023 Rick
4+
Copyright (c) 2023-2024 Rick
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy
77
of this software and associated documentation files (the "Software"), to deal
@@ -39,6 +39,8 @@ func TestRuntime(t *testing.T) {
3939
execer := NewDefaultExecer()
4040
assert.Equal(t, runtime.GOOS, execer.OS())
4141
assert.Equal(t, runtime.GOARCH, execer.Arch())
42+
assert.Nil(t, execer.Setenv("key", "value"))
43+
assert.Equal(t, "value", execer.Getenv("key"))
4244
}
4345

4446
func TestDefaultLookPath(t *testing.T) {

fake-command.go

+32-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
MIT License
33
4-
Copyright (c) 2023 Rick
4+
Copyright (c) 2023-2024 Rick
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy
77
of this software and associated documentation files (the "Software"), to deal
@@ -40,34 +40,35 @@ type FakeExecer struct {
4040
ExpectOS string
4141
ExpectArch string
4242
ExpectLookPath string
43+
Env map[string]string
4344
}
4445

45-
func (f FakeExecer) WithContext(ctx context.Context) Execer {
46+
func (f *FakeExecer) WithContext(ctx context.Context) Execer {
4647
return f
4748
}
4849

4950
// LookPath is a fake method
50-
func (f FakeExecer) LookPath(path string) (string, error) {
51+
func (f *FakeExecer) LookPath(path string) (string, error) {
5152
return f.ExpectLookPath, f.ExpectLookPathError
5253
}
5354

5455
// 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) {
5657
return []byte(f.ExpectOutput), f.ExpectError
5758
}
5859

5960
// RunCommand runs a command
60-
func (f FakeExecer) RunCommand(name string, arg ...string) error {
61+
func (f *FakeExecer) RunCommand(name string, arg ...string) error {
6162
return f.ExpectError
6263
}
6364

6465
// 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 {
6667
return f.ExpectError
6768
}
6869

6970
// 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) {
7172
if err = f.ExpectError; err == nil {
7273
result = f.ExpectOutput
7374
} else {
@@ -78,41 +79,57 @@ func (f FakeExecer) RunCommandAndReturn(name, dir string, args ...string) (resul
7879
}
7980

8081
// 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) {
8283
return f.ExpectError
8384
}
8485

8586
// 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 {
8788
return f.ExpectError
8889
}
8990

9091
// 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 {
9293
return f.ExpectError
9394
}
9495

9596
// 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 {
9798
return f.ExpectError
9899
}
99100

100101
// 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 {
102103
return f.ExpectError
103104
}
104105

105106
// 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 {
107108
return f.ExpectError
108109
}
109110

110111
// OS returns the os name
111-
func (f FakeExecer) OS() string {
112+
func (f *FakeExecer) OS() string {
112113
return f.ExpectOS
113114
}
114115

115116
// Arch returns the os arch
116-
func (f FakeExecer) Arch() string {
117+
func (f *FakeExecer) Arch() string {
117118
return f.ExpectArch
118119
}
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{}

fake-command_test.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
MIT License
33
4-
Copyright (c) 2023 Rick
4+
Copyright (c) 2023-2024 Rick
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy
77
of this software and associated documentation files (the "Software"), to deal
@@ -25,21 +25,23 @@ SOFTWARE.
2525
package exec
2626

2727
import (
28+
"context"
2829
"errors"
2930
"testing"
3031

3132
"github.com/stretchr/testify/assert"
3233
)
3334

3435
func TestLookPath(t *testing.T) {
35-
fake := FakeExecer{
36+
fake := &FakeExecer{
3637
ExpectLookPathError: errors.New("fake"),
3738
ExpectOutput: "output",
3839
ExpectErrOutput: "error",
3940
ExpectOS: "os",
4041
ExpectArch: "arch",
4142
ExpectLookPath: "lookpath",
4243
}
44+
fake.WithContext(context.Background())
4345
targetPath, err := fake.LookPath("")
4446
assert.NotNil(t, err)
4547
assert.Equal(t, "lookpath", targetPath)
@@ -77,4 +79,7 @@ func TestLookPath(t *testing.T) {
7779
assert.Equal(t, "outputerror", result)
7880
assert.NotNil(t, err)
7981
assert.Error(t, fakeWithErr.MkdirAll("", 0))
82+
83+
assert.Nil(t, fake.Setenv("key", "value"))
84+
assert.Equal(t, "value", fake.Getenv("key"))
8085
}

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/linuxsuren/go-fake-runtime
22

3-
go 1.18
3+
go 1.23
44

55
require github.com/stretchr/testify v1.8.2
66

0 commit comments

Comments
 (0)