-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
601 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
name: Generate code coverage badge | ||
|
||
# on: | ||
# pull_request: | ||
# branches: | ||
# - main | ||
|
||
on: [push] | ||
|
||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
name: Update coverage badge | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
with: | ||
persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of your personal access token. | ||
fetch-depth: 0 # otherwise, there would be errors pushing refs to the destination repository. | ||
|
||
- name: Setup go | ||
uses: actions/setup-go@v3 | ||
|
||
- uses: actions/cache@v3 | ||
with: | ||
path: | | ||
~/.cache/go-build | ||
~/go/pkg/mod | ||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} | ||
restore-keys: | | ||
${{ runner.os }}-go- | ||
- name: Run Test | ||
run: | | ||
go test -v ./... -covermode=count -coverprofile=coverage.out | ||
go tool cover -func=coverage.out -o=coverage.out | ||
- name: Go Coverage Badge # Pass the `coverage.out` output to this action | ||
uses: tj-actions/coverage-badge-go@v2 | ||
with: | ||
filename: coverage.out | ||
|
||
- name: Verify Changed files | ||
uses: tj-actions/verify-changed-files@v12 | ||
id: verify-changed-files | ||
with: | ||
files: README.md | ||
|
||
- name: Commit changes | ||
if: steps.verify-changed-files.outputs.files_changed == 'true' | ||
run: | | ||
git config --local user.email "[email protected]" | ||
git config --local user.name "GitHub Action" | ||
git add README.md | ||
git commit -m "chore: Updated coverage badge." | ||
- name: Push changes | ||
if: steps.verify-changed-files.outputs.files_changed == 'true' | ||
uses: ad-m/github-push-action@master | ||
with: | ||
github_token: ${{ github.token }} | ||
branch: ${{ github.head_ref }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
name: golangci-lint | ||
on: | ||
push: | ||
tags: | ||
- v* | ||
branches: | ||
- master | ||
- main | ||
pull_request: | ||
permissions: | ||
contents: read | ||
# Optional: allow read access to pull request. Use with `only-new-issues` option. | ||
# pull-requests: read | ||
jobs: | ||
golangci: | ||
name: lint | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/setup-go@v3 | ||
with: | ||
go-version: 1.19 | ||
- uses: actions/checkout@v3 | ||
- name: golangci-lint | ||
uses: golangci/golangci-lint-action@v3 | ||
with: | ||
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version | ||
version: 'latest' | ||
|
||
# Optional: working directory, useful for monorepos | ||
# working-directory: somedir | ||
|
||
# Optional: golangci-lint command line arguments. | ||
# args: --issues-exit-code=0 | ||
|
||
# Optional: show only new issues if it's a pull request. The default value is `false`. | ||
# only-new-issues: true | ||
|
||
# Optional: if set to true then the all caching functionality will be complete disabled, | ||
# takes precedence over all other caching options. | ||
# skip-cache: true | ||
|
||
# Optional: if set to true then the action don't cache or restore ~/go/pkg. | ||
# skip-pkg-cache: true | ||
|
||
# Optional: if set to true then the action don't cache or restore ~/.cache/go-build. | ||
# skip-build-cache: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
name: Go | ||
|
||
on: [push] | ||
|
||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v3 | ||
with: | ||
go-version: 1.19.x | ||
cache: true | ||
|
||
- name: Build | ||
run: go build -v ./... | ||
|
||
- name: Test | ||
run: go test -v ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package builtins | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
var ( | ||
ErrInvalidArgCount = errors.New("invalid argument count") | ||
HomeDir, _ = os.UserHomeDir() | ||
) | ||
|
||
func ChangeDirectory(args ...string) error { | ||
switch len(args) { | ||
case 0: // change to home directory if available | ||
if HomeDir == "" { | ||
return fmt.Errorf("%w: no homedir found, expected one argument (directory)", ErrInvalidArgCount) | ||
} | ||
return os.Chdir(HomeDir) | ||
case 1: | ||
return os.Chdir(args[0]) | ||
default: | ||
return fmt.Errorf("%w: expected zero or one arguments (directory)", ErrInvalidArgCount) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package builtins_test | ||
|
||
import ( | ||
"errors" | ||
"github.com/jh125486/CSCE4600/Project2/builtins" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestChangeDirectory(t *testing.T) { | ||
tmp := t.TempDir() | ||
|
||
type args struct { | ||
args []string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
unsetHomedir bool | ||
wantDir string | ||
wantErr error | ||
}{ | ||
{ | ||
name: "error too many args", | ||
args: args{ | ||
args: []string{"abc", "def"}, | ||
}, | ||
wantErr: builtins.ErrInvalidArgCount, | ||
}, | ||
{ | ||
name: "no args should change to homedir if available", | ||
wantDir: builtins.HomeDir, | ||
}, | ||
{ | ||
name: "no args should error if homedir is blank", | ||
unsetHomedir: true, | ||
wantErr: builtins.ErrInvalidArgCount, | ||
}, | ||
{ | ||
name: "one arg should change to dir", | ||
args: args{ | ||
args: []string{tmp}, | ||
}, | ||
wantDir: tmp, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
// setup | ||
if tt.unsetHomedir { | ||
oldVal := builtins.HomeDir | ||
t.Cleanup(func() { | ||
builtins.HomeDir = oldVal | ||
}) | ||
builtins.HomeDir = "" | ||
} | ||
|
||
// testing | ||
if err := builtins.ChangeDirectory(tt.args.args...); tt.wantErr != nil { | ||
if !errors.Is(err, tt.wantErr) { | ||
t.Fatalf("ChangeDirectory() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
return | ||
} else if err != nil { | ||
t.Fatalf("ChangeDirectory() unexpected error: %v", err) | ||
} | ||
|
||
// "happy" path | ||
wd, err := os.Getwd() | ||
if err != nil { | ||
t.Fatalf("Could not get working dir") | ||
} | ||
d1, err := os.Stat(wd) | ||
if err != nil { | ||
t.Fatalf("Could not stat dir: %v", wd) | ||
} | ||
d2, err := os.Stat(tt.wantDir) | ||
if err != nil { | ||
t.Fatalf("Could not stat dir: %v", tt.wantDir) | ||
} | ||
if !os.SameFile(d1, d2) { | ||
t.Errorf("Working Directory = %v, wantDir %v", wd, tt.wantDir) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package builtins | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
) | ||
|
||
func EnvironmentVariables(w io.Writer, args ...string) error { | ||
toRemove := make([]string, 0) | ||
for i := 0; i < len(args); i++ { | ||
if args[i] == "-u" { | ||
if len(args) < i+2 { | ||
return fmt.Errorf("%w: -u requires an argument", ErrInvalidArgCount) | ||
} | ||
toRemove = append(toRemove, args[i+1]) | ||
i++ | ||
} | ||
} | ||
|
||
toShow := make([]string, 0) | ||
for _, env := range os.Environ() { | ||
show := true | ||
for _, v := range toRemove { | ||
if strings.HasPrefix(env, v+"=") { | ||
show = false | ||
break | ||
} | ||
} | ||
if show { | ||
toShow = append(toShow, env) | ||
} | ||
} | ||
|
||
_, err := fmt.Fprintln(w, strings.Join(toShow, "\n")) | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package builtins | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestEnvironmentVariables(t *testing.T) { | ||
envs := os.Environ() | ||
type args struct { | ||
args []string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
setEnv map[string]string | ||
wantOut string | ||
wantErr error | ||
}{ | ||
{ | ||
name: "success no args", | ||
setEnv: map[string]string{ | ||
"ABCD": "FEG", | ||
}, | ||
wantOut: fmt.Sprintln(strings.Join(append(envs, "ABCD=FEG"), "\n")), | ||
}, | ||
{ | ||
name: "bad args", | ||
args: args{ | ||
args: []string{ | ||
"-u", | ||
}, | ||
}, | ||
wantErr: ErrInvalidArgCount, | ||
}, | ||
{ | ||
name: "success no args", | ||
setEnv: map[string]string{ | ||
"ABCD1": "FEG1", | ||
"ABCD2": "FEG2", | ||
"ABCD3": "FEG3", | ||
}, | ||
args: args{ | ||
args: []string{ | ||
"-u", "ABCD1", | ||
"-u", "ABCD2", | ||
}, | ||
}, | ||
wantOut: fmt.Sprintln(strings.Join(append(envs, "ABCD3=FEG3"), "\n")), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
// setup | ||
for k, v := range tt.setEnv { | ||
t.Setenv(k, v) | ||
} | ||
|
||
// test | ||
var out bytes.Buffer | ||
if err := EnvironmentVariables(&out, tt.args.args...); tt.wantErr != nil { | ||
if !errors.Is(err, tt.wantErr) { | ||
t.Fatalf("EnvironmentVariables() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
return | ||
} else if err != nil { | ||
t.Fatalf("EnvironmentVariables() unexpected error: %v", err) | ||
} | ||
if got := out.String(); got != tt.wantOut { | ||
t.Errorf("EnvironmentVariables() got = %v, want %v", got, tt.wantOut) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.