Skip to content

Commit

Permalink
[mkdir] barebones commands test structure
Browse files Browse the repository at this point in the history
  • Loading branch information
whouishere committed Jan 12, 2024
1 parent 2924641 commit 3ad74f1
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
build*/
tmp/
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ build:
run:
go run coreutils/$(UTIL)/$(UTIL).go $(ARGS)

test:
go test ./coreutils/...
test: build
PREFIX="$(BIN_PREFIX)" BUILD_DIR="$(shell pwd)/$(BUILD_DIR)" go test ./coreutils/...
go test ./internal/...

install:
Expand Down
33 changes: 33 additions & 0 deletions coreutils/mkdir/mkdir_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"os"
"testing"

"codeberg.org/whou/simpleutils/internal/test"
)

func TestParentsNoErrIfDirExists(t *testing.T) {
binPrefix := os.Getenv("PREFIX")
if binPrefix == "" {
t.Fatal("Required PREFIX environment variable not found.")
}

binDir := os.Getenv("BUILD_DIR")
if binDir == "" {
t.Fatal("Required BUILD_DIR environment variable not found.")
}

binPath := binDir + "/" + binPrefix + "mkdir"

// create directory
testDir := t.TempDir()

// try use mkdir in the already created directory with `-p` flag
out, code, err := test.GetCmdOutput(binPath, "-p", testDir)
t.Log(out)

if code != 0 {
t.Fatalf("Process ran with err \"%v\", expected exit status 0.", err)
}
}
15 changes: 11 additions & 4 deletions internal/test/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,17 @@ func GetFuncOutput(function func(), testName string) (string, error) {
return string(out), err
}

// get a command's stdout
func GetCmdOutput(command string) (string, error) {
cmd := exec.Command(command)
// get a command's stdout and exit code
func GetCmdOutput(command string, arg ...string) (string, int, error) {
cmd := exec.Command(command, arg...)
out, err := cmd.CombinedOutput()

return string(out), err
var code int
if exitErr, ok := err.(*exec.ExitError); ok {
code = exitErr.ExitCode()
} else if err != nil {
code = -1
}

return string(out), code, err
}

0 comments on commit 3ad74f1

Please sign in to comment.