From 605d8e1178b0b47018c32a60b3572715daa994a1 Mon Sep 17 00:00:00 2001 From: rick Date: Tue, 24 Dec 2024 10:20:00 +0800 Subject: [PATCH] feat: support to get and set env --- .github/workflows/build.yaml | 2 +- .gitignore | 1 + command.go | 14 ++++++++++- command_test.go | 4 ++- fake-command.go | 47 ++++++++++++++++++++++++------------ fake-command_test.go | 9 +++++-- go.mod | 2 +- 7 files changed, 58 insertions(+), 21 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index d2a685f..5a7c41a 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -11,7 +11,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v3 with: - go-version: 1.18.x + go-version: 1.23.x - uses: actions/checkout@v3.0.0 - name: Test run: | diff --git a/.gitignore b/.gitignore index 2d83068..e9eb644 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ coverage.out +.idea/ diff --git a/command.go b/command.go index 8b886d2..cdb152b 100644 --- a/command.go +++ b/command.go @@ -1,7 +1,7 @@ /* MIT License -Copyright (c) 2023 Rick +Copyright (c) 2023-2024 Rick Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -50,6 +50,8 @@ type Execer interface { MkdirAll(path string, perm os.FileMode) error OS() string Arch() string + Getenv(string) string + Setenv(string, string) error WithContext(context.Context) Execer } @@ -184,6 +186,16 @@ func (e *defaultExecer) Arch() string { return runtime.GOARCH } +// Getenv returns the env value by key +func (e *defaultExecer) Getenv(key string) string { + return os.Getenv(key) +} + +// Setenv sets the key-value pair into the env +func (e *defaultExecer) Setenv(key, value string) error { + return os.Setenv(key, value) +} + // RunCommandAndReturn runs a command, then returns the output func (e *defaultExecer) RunCommandAndReturn(name, dir string, args ...string) (result string, err error) { stdout := &bytes.Buffer{} diff --git a/command_test.go b/command_test.go index 19e31ae..9833ca1 100644 --- a/command_test.go +++ b/command_test.go @@ -1,7 +1,7 @@ /* MIT License -Copyright (c) 2023 Rick +Copyright (c) 2023-2024 Rick Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -39,6 +39,8 @@ func TestRuntime(t *testing.T) { execer := NewDefaultExecer() assert.Equal(t, runtime.GOOS, execer.OS()) assert.Equal(t, runtime.GOARCH, execer.Arch()) + assert.Nil(t, execer.Setenv("key", "value")) + assert.Equal(t, "value", execer.Getenv("key")) } func TestDefaultLookPath(t *testing.T) { diff --git a/fake-command.go b/fake-command.go index 66b1d2d..00df408 100644 --- a/fake-command.go +++ b/fake-command.go @@ -1,7 +1,7 @@ /* MIT License -Copyright (c) 2023 Rick +Copyright (c) 2023-2024 Rick Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -40,34 +40,35 @@ type FakeExecer struct { ExpectOS string ExpectArch string ExpectLookPath string + Env map[string]string } -func (f FakeExecer) WithContext(ctx context.Context) Execer { +func (f *FakeExecer) WithContext(ctx context.Context) Execer { return f } // LookPath is a fake method -func (f FakeExecer) LookPath(path string) (string, error) { +func (f *FakeExecer) LookPath(path string) (string, error) { return f.ExpectLookPath, f.ExpectLookPathError } // Command is a fake method -func (f FakeExecer) Command(name string, arg ...string) ([]byte, error) { +func (f *FakeExecer) Command(name string, arg ...string) ([]byte, error) { return []byte(f.ExpectOutput), f.ExpectError } // RunCommand runs a command -func (f FakeExecer) RunCommand(name string, arg ...string) error { +func (f *FakeExecer) RunCommand(name string, arg ...string) error { return f.ExpectError } // RunCommandInDir is a fake method -func (f FakeExecer) RunCommandInDir(name, dir string, args ...string) error { +func (f *FakeExecer) RunCommandInDir(name, dir string, args ...string) error { return f.ExpectError } // RunCommandAndReturn is a fake method -func (f FakeExecer) RunCommandAndReturn(name, dir string, args ...string) (result string, err error) { +func (f *FakeExecer) RunCommandAndReturn(name, dir string, args ...string) (result string, err error) { if err = f.ExpectError; err == nil { result = f.ExpectOutput } else { @@ -78,41 +79,57 @@ func (f FakeExecer) RunCommandAndReturn(name, dir string, args ...string) (resul } // RunCommandWithSudo is a fake method -func (f FakeExecer) RunCommandWithSudo(name string, args ...string) (err error) { +func (f *FakeExecer) RunCommandWithSudo(name string, args ...string) (err error) { return f.ExpectError } // RunCommandWithBuffer is a fake method -func (f FakeExecer) RunCommandWithBuffer(name, dir string, stdout, stderr *bytes.Buffer, args ...string) error { +func (f *FakeExecer) RunCommandWithBuffer(name, dir string, stdout, stderr *bytes.Buffer, args ...string) error { return f.ExpectError } // RunCommandWithIO is a fake method -func (f FakeExecer) RunCommandWithIO(name, dir string, stdout, stderr io.Writer, p chan Process, args ...string) error { +func (f *FakeExecer) RunCommandWithIO(name, dir string, stdout, stderr io.Writer, p chan Process, args ...string) error { return f.ExpectError } // RunCommandWithEnv is a fake method -func (f FakeExecer) RunCommandWithEnv(name string, argv, envv []string, stdout, stderr io.Writer) error { +func (f *FakeExecer) RunCommandWithEnv(name string, argv, envv []string, stdout, stderr io.Writer) error { return f.ExpectError } // SystemCall is a fake method -func (f FakeExecer) SystemCall(name string, argv []string, envv []string) error { +func (f *FakeExecer) SystemCall(name string, argv []string, envv []string) error { return f.ExpectError } // MkdirAll is the wrapper of os.MkdirAll -func (f FakeExecer) MkdirAll(path string, perm os.FileMode) error { +func (f *FakeExecer) MkdirAll(path string, perm os.FileMode) error { return f.ExpectError } // OS returns the os name -func (f FakeExecer) OS() string { +func (f *FakeExecer) OS() string { return f.ExpectOS } // Arch returns the os arch -func (f FakeExecer) Arch() string { +func (f *FakeExecer) Arch() string { return f.ExpectArch } + +// Getenv returns the env value by key +func (f *FakeExecer) Getenv(key string) string { + return f.Env[key] +} + +// Setenv sets the key-value pair into the env +func (f *FakeExecer) Setenv(key, value string) error { + if f.Env == nil { + f.Env = make(map[string]string) + } + f.Env[key] = value + return nil +} + +var _ Execer = &FakeExecer{} diff --git a/fake-command_test.go b/fake-command_test.go index 3bb80ac..e39c505 100644 --- a/fake-command_test.go +++ b/fake-command_test.go @@ -1,7 +1,7 @@ /* MIT License -Copyright (c) 2023 Rick +Copyright (c) 2023-2024 Rick Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -25,6 +25,7 @@ SOFTWARE. package exec import ( + "context" "errors" "testing" @@ -32,7 +33,7 @@ import ( ) func TestLookPath(t *testing.T) { - fake := FakeExecer{ + fake := &FakeExecer{ ExpectLookPathError: errors.New("fake"), ExpectOutput: "output", ExpectErrOutput: "error", @@ -40,6 +41,7 @@ func TestLookPath(t *testing.T) { ExpectArch: "arch", ExpectLookPath: "lookpath", } + fake.WithContext(context.Background()) targetPath, err := fake.LookPath("") assert.NotNil(t, err) assert.Equal(t, "lookpath", targetPath) @@ -77,4 +79,7 @@ func TestLookPath(t *testing.T) { assert.Equal(t, "outputerror", result) assert.NotNil(t, err) assert.Error(t, fakeWithErr.MkdirAll("", 0)) + + assert.Nil(t, fake.Setenv("key", "value")) + assert.Equal(t, "value", fake.Getenv("key")) } diff --git a/go.mod b/go.mod index cec2c99..a756508 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/linuxsuren/go-fake-runtime -go 1.18 +go 1.23 require github.com/stretchr/testify v1.8.2