Skip to content

Commit

Permalink
Merge pull request #39 from crazy-max/hardened-tests
Browse files Browse the repository at this point in the history
test: remove hardcoded files to check
  • Loading branch information
crazy-max authored Feb 21, 2024
2 parents 3149c91 + 04f2e85 commit 522da51
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 25 deletions.
44 changes: 34 additions & 10 deletions clidocstool_md_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@
package clidocstool

import (
"io/fs"
"os"
"path"
"path/filepath"
"strings"
"testing"

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

//nolint:errcheck
func TestGenMarkdownTree(t *testing.T) {
tmpdir := t.TempDir()

err := copyFile(path.Join("fixtures", "buildx_stop.pre.md"), path.Join(tmpdir, "buildx_stop.md"))
require.NoError(t, err)
require.NoError(t, copyFile(path.Join("fixtures", "buildx_stop.pre.md"), path.Join(tmpdir, "buildx_stop.md")))

c, err := New(Options{
Root: buildxCmd,
Expand All @@ -39,15 +39,39 @@ func TestGenMarkdownTree(t *testing.T) {
require.NoError(t, err)
require.NoError(t, c.GenMarkdownTree(buildxCmd))

for _, tt := range []string{"buildx.md", "buildx_build.md", "buildx_stop.md"} {
tt := tt
t.Run(tt, func(t *testing.T) {
bres, err := os.ReadFile(filepath.Join(tmpdir, tt))
seen := make(map[string]struct{})

filepath.Walk("fixtures", func(path string, info fs.FileInfo, err error) error {
fname := filepath.Base(path)
// ignore dirs, .pre.md files and any file that is not a .md file
if info.IsDir() || !strings.HasSuffix(fname, ".md") || strings.HasSuffix(fname, ".pre.md") {
return nil
}
t.Run(fname, func(t *testing.T) {
seen[fname] = struct{}{}
require.NoError(t, err)

bres, err := os.ReadFile(filepath.Join(tmpdir, fname))
require.NoError(t, err)

bexc, err := os.ReadFile(path.Join("fixtures", tt))
bexc, err := os.ReadFile(path)
require.NoError(t, err)
assert.Equal(t, string(bexc), string(bres))
require.Equal(t, string(bexc), string(bres))
})
}
return nil
})

filepath.Walk(tmpdir, func(path string, info fs.FileInfo, err error) error {
fname := filepath.Base(path)
// ignore dirs, .pre.md files and any file that is not a .md file
if info.IsDir() || !strings.HasSuffix(fname, ".md") || strings.HasSuffix(fname, ".pre.md") {
return nil
}
t.Run("seen_"+fname, func(t *testing.T) {
if _, ok := seen[fname]; !ok {
t.Errorf("file %s not found in fixtures", fname)
}
})
return nil
})
}
41 changes: 33 additions & 8 deletions clidocstool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
package clidocstool

import (
"io/fs"
"os"
"path"
"path/filepath"
"strings"
"testing"

"github.com/docker/cli-docs-tool/annotation"
Expand Down Expand Up @@ -192,8 +194,7 @@ format: "default|<id>[=<socket>|<key>[,<key>]]"`)
func TestGenAllTree(t *testing.T) {
tmpdir := t.TempDir()

err := copyFile(path.Join("fixtures", "buildx_stop.pre.md"), path.Join(tmpdir, "buildx_stop.md"))
require.NoError(t, err)
require.NoError(t, copyFile(path.Join("fixtures", "buildx_stop.pre.md"), path.Join(tmpdir, "buildx_stop.md")))

c, err := New(Options{
Root: buildxCmd,
Expand All @@ -203,15 +204,39 @@ func TestGenAllTree(t *testing.T) {
require.NoError(t, err)
require.NoError(t, c.GenAllTree())

for _, tt := range []string{"buildx.md", "buildx_build.md", "buildx_stop.md", "docker_buildx.yaml", "docker_buildx_build.yaml", "docker_buildx_install.yaml", "docker_buildx_stop.yaml"} {
tt := tt
t.Run(tt, func(t *testing.T) {
bres, err := os.ReadFile(filepath.Join(tmpdir, tt))
seen := make(map[string]struct{})

filepath.Walk("fixtures", func(path string, info fs.FileInfo, err error) error {
fname := filepath.Base(path)
// ignore dirs and .pre.md files
if info.IsDir() || strings.HasSuffix(fname, ".pre.md") {
return nil
}
t.Run(fname, func(t *testing.T) {
seen[fname] = struct{}{}
require.NoError(t, err)

bexc, err := os.ReadFile(path.Join("fixtures", tt))
bres, err := os.ReadFile(filepath.Join(tmpdir, fname))
require.NoError(t, err)

bexc, err := os.ReadFile(path)
require.NoError(t, err)
assert.Equal(t, string(bexc), string(bres))
})
}
return nil
})

filepath.Walk(tmpdir, func(path string, info fs.FileInfo, err error) error {
fname := filepath.Base(path)
// ignore dirs and .pre.md files
if info.IsDir() || strings.HasSuffix(fname, ".pre.md") {
return nil
}
t.Run("seen_"+fname, func(t *testing.T) {
if _, ok := seen[fname]; !ok {
t.Errorf("file %s not found in fixtures", fname)
}
})
return nil
})
}
39 changes: 32 additions & 7 deletions clidocstool_yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
package clidocstool

import (
"io/fs"
"os"
"path"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -36,15 +37,39 @@ func TestGenYamlTree(t *testing.T) {
require.NoError(t, err)
require.NoError(t, c.GenYamlTree(buildxCmd))

for _, tt := range []string{"docker_buildx.yaml", "docker_buildx_build.yaml", "docker_buildx_install.yaml", "docker_buildx_stop.yaml"} {
tt := tt
t.Run(tt, func(t *testing.T) {
bres, err := os.ReadFile(filepath.Join(tmpdir, tt))
seen := make(map[string]struct{})

filepath.Walk("fixtures", func(path string, info fs.FileInfo, err error) error {
fname := filepath.Base(path)
// ignore dirs and any file that is not a .yaml file
if info.IsDir() || !strings.HasSuffix(fname, ".yaml") {
return nil
}
t.Run(fname, func(t *testing.T) {
seen[fname] = struct{}{}
require.NoError(t, err)

bres, err := os.ReadFile(filepath.Join(tmpdir, fname))
require.NoError(t, err)

bexc, err := os.ReadFile(path.Join("fixtures", tt))
bexc, err := os.ReadFile(path)
require.NoError(t, err)
assert.Equal(t, string(bexc), string(bres))
})
}
return nil
})

filepath.Walk(tmpdir, func(path string, info fs.FileInfo, err error) error {
fname := filepath.Base(path)
// ignore dirs and any file that is not a .yaml file
if info.IsDir() || !strings.HasSuffix(fname, ".yaml") {
return nil
}
t.Run("seen_"+fname, func(t *testing.T) {
if _, ok := seen[fname]; !ok {
t.Errorf("file %s not found in fixtures", fname)
}
})
return nil
})
}

0 comments on commit 522da51

Please # to comment.