Skip to content

Commit

Permalink
Merge pull request #9 from LinuxSuRen/feat/env
Browse files Browse the repository at this point in the history
feat: support to get and set env
  • Loading branch information
LinuxSuRen authored Dec 24, 2024
2 parents a36148d + 605d8e1 commit 9865dbb
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
coverage.out
.idea/
14 changes: 13 additions & 1 deletion command.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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{}
Expand Down
4 changes: 3 additions & 1 deletion command_test.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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) {
Expand Down
47 changes: 32 additions & 15 deletions fake-command.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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{}
9 changes: 7 additions & 2 deletions fake-command_test.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -25,21 +25,23 @@ SOFTWARE.
package exec

import (
"context"
"errors"
"testing"

"github.com/stretchr/testify/assert"
)

func TestLookPath(t *testing.T) {
fake := FakeExecer{
fake := &FakeExecer{
ExpectLookPathError: errors.New("fake"),
ExpectOutput: "output",
ExpectErrOutput: "error",
ExpectOS: "os",
ExpectArch: "arch",
ExpectLookPath: "lookpath",
}
fake.WithContext(context.Background())
targetPath, err := fake.LookPath("")
assert.NotNil(t, err)
assert.Equal(t, "lookpath", targetPath)
Expand Down Expand Up @@ -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"))
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit 9865dbb

Please # to comment.