Skip to content

Commit

Permalink
Merge pull request #328 from metrumresearchgroup/more-tests
Browse files Browse the repository at this point in the history
Add a few tests
  • Loading branch information
kyleam authored Aug 16, 2024
2 parents dd653b9 + e759db3 commit 7d5e572
Show file tree
Hide file tree
Showing 160 changed files with 538 additions and 20 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ jobs:
-ldflags "-X github.com/metrumresearchgroup/bbi/cmd.VERSION=$version" \
cmd/bbi/main.go
echo "$bin" >>$GITHUB_PATH
- name: Integration tests (postrun)
- name: Integration tests (without NONMEM)
shell: bash
run: |
bbi version
go test ./integration/postrun
go test ./integration/nmless
release:
if: github.ref_type == 'tag'
name: Make release
Expand Down
12 changes: 8 additions & 4 deletions cmd/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package cmd

import (
"errors"
"fmt"
"os"
"path/filepath"

Expand Down Expand Up @@ -47,8 +46,6 @@ func probs(_ *cobra.Command, args []string) error {
case 1:
dirPath = args[0]
default:
fmt.Println("currently only supports scanning one directory")

return errors.New("project only supports specifying one directory")
}

Expand All @@ -58,7 +55,14 @@ func probs(_ *cobra.Command, args []string) error {
if err != nil {
return err
}
modelSummaries := modSummaries(AppFs, modelFiles)

filesAbs := make([]string, len(modelFiles))
for i := range modelFiles {
filesAbs[i] = filepath.Join(dirPath, modelFiles[i])
}

modelSummaries := modSummaries(AppFs, filesAbs)

if Json {
err = utils.PrintJSON(modelSummaries)
if err != nil {
Expand Down
20 changes: 12 additions & 8 deletions docs/validation/matrix.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,35 @@
- entrypoint: bbi nonmem clean
code: cmd/clean.go
doc: docs/commands/bbi_nonmem_clean.md
tests: []
tests:
- integration/nmless/bbi_clean_test.go

- entrypoint: bbi nonmem covcor
code: cmd/covcor.go
doc: docs/commands/bbi_nonmem_covcor.md
tests:
- integration/postrun/bbi_covcor_test.go
- integration/nmless/bbi_covcor_test.go
- parsers/nmparser/read_cov_test.go

- entrypoint: bbi nonmem params
code: cmd/params.go
doc: docs/commands/bbi_nonmem_params.md
tests:
- cmd/params_test.go
- integration/postrun/bbi_params_test.go
- integration/nmless/bbi_params_test.go
- parsers/nmparser/read_ext_fast_test.go

- entrypoint: bbi nonmem probs
code: cmd/project.go
doc: docs/commands/bbi_nonmem_probs.md
tests: []
tests:
- integration/nmless/bbi_probs_test.go

- entrypoint: bbi nonmem reclean
code: cmd/reclean.go
doc: docs/commands/bbi_nonmem_reclean.md
tests:
- integration/postrun/bbi_reclean_test.go
- integration/nmless/bbi_reclean_test.go

- entrypoint: bbi nonmem run
skip: true
Expand Down Expand Up @@ -69,13 +71,14 @@
- entrypoint: bbi nonmem scaffold
code: cmd/scaffold.go
doc: docs/commands/bbi_nonmem_scaffold.md
tests: []
tests:
- integration/nmless/bbi_scaffold_test.go

- entrypoint: bbi nonmem summary
code: cmd/summary.go
doc: docs/commands/bbi_nonmem_summary.md
tests:
- integration/postrun/bbi_summary_test.go
- integration/nmless/bbi_summary_test.go
- parsers/nmparser/parse_block_result_test.go
- parsers/nmparser/parse_final_parameter_estimates_test.go
- parsers/nmparser/parse_lst_file_test.go
Expand All @@ -88,4 +91,5 @@
- entrypoint: bbi version
code: cmd/version.go
doc: docs/commands/bbi_version.md
tests: []
tests:
- integration/nmless/bbi_version_test.go
File renamed without changes.
287 changes: 287 additions & 0 deletions integration/nmless/bbi_clean_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,287 @@
package nmless

import (
"encoding/json"
"os"
"os/exec"
"path/filepath"
"testing"

"github.com/metrumresearchgroup/wrapt"

"github.com/metrumresearchgroup/bbi/runner"
)

func createEmptyFile(t *wrapt.T, file string) {
t.Helper()
fh, err := os.Create(file)
if err != nil {
t.Fatal(err)
}
err = fh.Close()
if err != nil {
t.Fatal(err)
}
}

func TestClean(tt *testing.T) {
t := wrapt.WrapT(tt)

t.Run("glob", func(t *wrapt.T) {
dir := t.TempDir()

foo := filepath.Join(dir, "foo")
fooctl := filepath.Join(dir, "foo.ctl")
foomod := filepath.Join(dir, "foo.mod")

createEmptyFile(t, foo)
createEmptyFile(t, fooctl)
createEmptyFile(t, foomod)

cmd := exec.Command("bbi", "nonmem", "clean", "*.ctl", "*.mod")
cmd.Dir = dir

_, err := cmd.Output()
if err != nil {
t.Fatal(err)
}

t.A.FileExists(foo)
t.A.NoFileExists(fooctl)
t.A.NoFileExists(foomod)
})

t.Run("regex", func(t *wrapt.T) {
dir := t.TempDir()

fooctl := filepath.Join(dir, "foo.ctl")
foolmod := filepath.Join(dir, "foolmod")
foomod := filepath.Join(dir, "foo.mod")
foomodulo := filepath.Join(dir, "foo.modulo")

createEmptyFile(t, fooctl)
createEmptyFile(t, foolmod)
createEmptyFile(t, foomod)
createEmptyFile(t, foomodulo)

cmd := exec.Command("bbi", "nonmem", "clean", "--regex", ".*\\.ctl", ".*\\.mod$")
cmd.Dir = dir

_, err := cmd.Output()
if err != nil {
t.Fatal(err)
}

t.A.NoFileExists(fooctl)
t.A.FileExists(foolmod)
t.A.NoFileExists(foomod)
t.A.FileExists(foomodulo)
})

t.Run("glob inverse", func(t *wrapt.T) {
dir := t.TempDir()

foo := filepath.Join(dir, "foo")
fooctl := filepath.Join(dir, "foo.ctl")

createEmptyFile(t, foo)
createEmptyFile(t, fooctl)

cmd := exec.Command("bbi", "nonmem", "clean", "--inverse", "*.ctl")
cmd.Dir = dir

_, err := cmd.Output()
if err != nil {
t.Fatal(err)
}

t.A.NoFileExists(foo)
t.A.FileExists(fooctl)
})

t.Run("regex inverse", func(t *wrapt.T) {
dir := t.TempDir()

barfoo := filepath.Join(dir, "barfoo.mod")
foomodulo := filepath.Join(dir, "foo.modulo")

createEmptyFile(t, foomodulo)
createEmptyFile(t, barfoo)

cmd := exec.Command("bbi", "nonmem", "clean", "--regex", "--inverse", "^foo.*mod")
cmd.Dir = dir

_, err := cmd.Output()
if err != nil {
t.Fatal(err)
}

t.A.NoFileExists(barfoo)
t.A.FileExists(foomodulo)
})

t.Run("dirsOnly", func(t *wrapt.T) {
dir := t.TempDir()

foodir := filepath.Join(dir, "foo")
foomod := filepath.Join(dir, "foo.mod")

err := os.Mkdir(foodir, 0o777)
if err != nil {
t.Fatal(err)
}

createEmptyFile(t, foomod)

cmd := exec.Command("bbi", "nonmem", "clean", "--dirsOnly", "foo*")
cmd.Dir = dir

_, err = cmd.Output()
if err != nil {
t.Fatal(err)
}

t.A.NoDirExists(foodir)
t.A.FileExists(foomod)
})

t.Run("filesOnly", func(t *wrapt.T) {
dir := t.TempDir()

foodir := filepath.Join(dir, "foo")
foomod := filepath.Join(dir, "foo.mod")

err := os.Mkdir(foodir, 0o777)
if err != nil {
t.Fatal(err)
}

createEmptyFile(t, foomod)

cmd := exec.Command("bbi", "nonmem", "clean", "--filesOnly", "foo*")
cmd.Dir = dir

_, err = cmd.Output()
if err != nil {
t.Fatal(err)
}

t.A.DirExists(foodir)
t.A.NoFileExists(foomod)
})
}

func writeCopied(t *wrapt.T, prefix string, files []runner.TargetedFile) {
t.Helper()

bs, err := json.Marshal(files)
if err != nil {
t.Fatal(err)
}
err = os.WriteFile(prefix+"_copied.json", bs, 0o666)
if err != nil {
t.Fatal(err)
}
}

func TestCleanCopiedRuns(tt *testing.T) {
t := wrapt.WrapT(tt)
dir := t.TempDir()

run01 := filepath.Join(dir, "run01.mod")
run01foo := filepath.Join(dir, "run01.foo")
run02 := filepath.Join(dir, "run02.mod")
run02bar := filepath.Join(dir, "run02.bar")
run03 := filepath.Join(dir, "run03.mod")
run03keep := filepath.Join(dir, "run03.keep")
run10 := filepath.Join(dir, "run10.mod")
run10baz := filepath.Join(dir, "run10.baz")

createEmptyFile(t, run01)
createEmptyFile(t, run01foo)
createEmptyFile(t, run02)
createEmptyFile(t, run02bar)
createEmptyFile(t, run03)
createEmptyFile(t, run03keep)
createEmptyFile(t, run10)
createEmptyFile(t, run10baz)

writeCopied(t, filepath.Join(dir, "run01"),
[]runner.TargetedFile{
{
File: "run01.foo",
},
},
)
writeCopied(t, filepath.Join(dir, "run02"),
[]runner.TargetedFile{
{
File: "run02.bar",
},
},
)
writeCopied(t, filepath.Join(dir, "run03"),
[]runner.TargetedFile{
{
File: "run03.keep",
},
},
)
writeCopied(t, filepath.Join(dir, "run10"),
[]runner.TargetedFile{
{
File: "run10.baz",
},
},
)

cmd := exec.Command("bbi", "nonmem", "clean", "--copiedRuns=run[01:02],run10")
cmd.Dir = dir

_, err := cmd.Output()
if err != nil {
t.Fatal(err)
}

t.A.FileExists(run01)
t.A.NoFileExists(run01foo)

t.A.FileExists(run02)
t.A.NoFileExists(run02bar)

t.A.FileExists(run03)
t.A.FileExists(run03keep)

t.A.FileExists(run10)
t.A.NoFileExists(run10baz)
}

func TestCleanPreview(tt *testing.T) {
t := wrapt.WrapT(tt)
dir := t.TempDir()

fooctl := filepath.Join(dir, "foo.ctl")
foomod := filepath.Join(dir, "foo.mod")
other := filepath.Join(dir, "other")

createEmptyFile(t, fooctl)
createEmptyFile(t, foomod)
createEmptyFile(t, other)

cmd := exec.Command("bbi", "nonmem", "clean", "--preview", "*.ctl", "*.mod")
cmd.Dir = dir

bs, err := cmd.Output()
if err != nil {
t.Fatal(err)
}
out := string(bs)

t.A.Contains(out, "foo.ctl")
t.A.Contains(out, "foo.mod")
t.A.NotContains(out, "other")

t.A.FileExists(fooctl)
t.A.FileExists(foomod)
t.A.FileExists(other)
}
Loading

0 comments on commit 7d5e572

Please sign in to comment.