Skip to content

Commit

Permalink
Make file paths OS-agnostic
Browse files Browse the repository at this point in the history
  • Loading branch information
SBGoods committed Mar 5, 2024
1 parent 86eb271 commit 6fefacb
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 42 deletions.
18 changes: 9 additions & 9 deletions internal/check/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func NumberOfFilesCheck(docFiles []string) error {
continue
}

if directory == RegistryIndexDirectory || directory == LegacyIndexDirectory {
if directory == RegistryIndexDirectory || directory == filepath.FromSlash(LegacyIndexDirectory) {
continue
}

Expand All @@ -155,7 +155,7 @@ func NumberOfFilesCheck(docFiles []string) error {

func IsValidLegacyDirectory(directory string) bool {
for _, validLegacyDirectory := range ValidLegacyDirectories {
if directory == validLegacyDirectory {
if directory == filepath.FromSlash(validLegacyDirectory) {
return true
}
}
Expand All @@ -165,7 +165,7 @@ func IsValidLegacyDirectory(directory string) bool {

func IsValidRegistryDirectory(directory string) bool {
for _, validRegistryDirectory := range ValidRegistryDirectories {
if directory == validRegistryDirectory {
if directory == filepath.FromSlash(validRegistryDirectory) {
return true
}
}
Expand All @@ -174,32 +174,32 @@ func IsValidRegistryDirectory(directory string) bool {
}

func IsValidCdktfDirectory(directory string) bool {
if directory == fmt.Sprintf("%s/%s", LegacyIndexDirectory, CdktfIndexDirectory) {
if directory == filepath.FromSlash(fmt.Sprintf("%s/%s", LegacyIndexDirectory, CdktfIndexDirectory)) {
return true
}

if directory == fmt.Sprintf("%s/%s", RegistryIndexDirectory, CdktfIndexDirectory) {
if directory == filepath.FromSlash(fmt.Sprintf("%s/%s", RegistryIndexDirectory, CdktfIndexDirectory)) {
return true
}

for _, validCdktfLanguage := range ValidCdktfLanguages {

if directory == fmt.Sprintf("%s/%s/%s", LegacyIndexDirectory, CdktfIndexDirectory, validCdktfLanguage) {
if directory == filepath.FromSlash(fmt.Sprintf("%s/%s/%s", LegacyIndexDirectory, CdktfIndexDirectory, validCdktfLanguage)) {
return true
}

if directory == fmt.Sprintf("%s/%s/%s", RegistryIndexDirectory, CdktfIndexDirectory, validCdktfLanguage) {
if directory == filepath.FromSlash(fmt.Sprintf("%s/%s/%s", RegistryIndexDirectory, CdktfIndexDirectory, validCdktfLanguage)) {
return true
}

for _, validLegacySubdirectory := range ValidLegacySubdirectories {
if directory == fmt.Sprintf("%s/%s/%s/%s", LegacyIndexDirectory, CdktfIndexDirectory, validCdktfLanguage, validLegacySubdirectory) {
if directory == filepath.FromSlash(fmt.Sprintf("%s/%s/%s/%s", LegacyIndexDirectory, CdktfIndexDirectory, validCdktfLanguage, validLegacySubdirectory)) {
return true
}
}

for _, validRegistrySubdirectory := range ValidRegistrySubdirectories {
if directory == fmt.Sprintf("%s/%s/%s/%s", RegistryIndexDirectory, CdktfIndexDirectory, validCdktfLanguage, validRegistrySubdirectory) {
if directory == filepath.FromSlash(fmt.Sprintf("%s/%s/%s/%s", RegistryIndexDirectory, CdktfIndexDirectory, validCdktfLanguage, validRegistrySubdirectory)) {
return true
}
}
Expand Down
5 changes: 3 additions & 2 deletions internal/check/directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package check
import (
"fmt"
"os"
"path/filepath"
"testing"

"github.com/bmatcuk/doublestar/v4"
Expand Down Expand Up @@ -60,11 +61,11 @@ func TestMixedDirectoriesCheck(t *testing.T) {
}{
{
Name: "valid mixed directories",
BasePath: "testdata/valid-mixed-directories",
BasePath: filepath.Join("testdata", "valid-mixed-directories"),
},
{
Name: "invalid mixed directories",
BasePath: "testdata/invalid-mixed-directories",
BasePath: filepath.Join("testdata", "invalid-mixed-directories"),
ExpectError: true,
},
}
Expand Down
5 changes: 3 additions & 2 deletions internal/check/file_mismatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package check

import (
"path/filepath"
"reflect"
"testing"
"testing/fstest"
Expand Down Expand Up @@ -73,12 +74,12 @@ func TestFileResourceName(t *testing.T) {
},
{
Name: "full path with single extensions",
File: "docs/resource/thing.md",
File: filepath.Join("docs", "resource", "thing.md"),
Expect: "test_thing",
},
{
Name: "full path with multiple extensions",
File: "website/docs/r/thing.html.markdown",
File: filepath.Join("website", "docs", "r", "thing.html.markdown"),
Expect: "test_thing",
},
}
Expand Down
13 changes: 7 additions & 6 deletions internal/check/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package check

import (
"os"
"path/filepath"
"testing"
)

Expand Down Expand Up @@ -72,16 +73,16 @@ func TestFullPath(t *testing.T) {
{
Name: "without base path",
FileOptions: &FileOptions{},
Path: "docs/resources/thing.md",
Expect: "docs/resources/thing.md",
Path: filepath.FromSlash("docs/resources/thing.md"),
Expect: filepath.FromSlash("docs/resources/thing.md"),
},
{
Name: "without base path",
Name: "with base path",
FileOptions: &FileOptions{
BasePath: "/full/path/to",
BasePath: filepath.FromSlash("/full/path/to"),
},
Path: "docs/resources/thing.md",
Expect: "/full/path/to/docs/resources/thing.md",
Path: filepath.FromSlash("docs/resources/thing.md"),
Expect: filepath.FromSlash("/full/path/to/docs/resources/thing.md"),
},
}

Expand Down
5 changes: 2 additions & 3 deletions internal/provider/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"
Expand Down Expand Up @@ -455,7 +454,7 @@ func (g *generator) renderStaticWebsite(providerSchema *tfjson.ProviderSchema) e
// Remove subdirectories managed by tfplugindocs
if file.IsDir() && slices.Contains(managedWebsiteSubDirectories, file.Name()) {
g.infof("removing directory: %q", file.Name())
err = os.RemoveAll(path.Join(g.ProviderDocsDir(), file.Name()))
err = os.RemoveAll(filepath.Join(g.ProviderDocsDir(), file.Name()))
if err != nil {
return fmt.Errorf("unable to remove directory %q from rendered website directory: %w", file.Name(), err)
}
Expand All @@ -465,7 +464,7 @@ func (g *generator) renderStaticWebsite(providerSchema *tfjson.ProviderSchema) e
// Remove files managed by tfplugindocs
if !file.IsDir() && slices.Contains(managedWebsiteFiles, file.Name()) {
g.infof("removing file: %q", file.Name())
err = os.RemoveAll(path.Join(g.ProviderDocsDir(), file.Name()))
err = os.RemoveAll(filepath.Join(g.ProviderDocsDir(), file.Name()))
if err != nil {
return fmt.Errorf("unable to remove file %q from rendered website directory: %w", file.Name(), err)
}
Expand Down
17 changes: 8 additions & 9 deletions internal/provider/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"errors"
"fmt"
"os"
"path"
"path/filepath"

"github.com/bmatcuk/doublestar/v4"
Expand Down Expand Up @@ -126,15 +125,15 @@ func (v *validator) validate(ctx context.Context) error {
err = check.NumberOfFilesCheck(files)
result = errors.Join(result, err)

if dirExists(path.Join(v.providerDir, "docs")) {
if dirExists(filepath.Join(v.providerDir, "docs")) {
v.logger.infof("detected static docs directory, running checks")
err = v.validateStaticDocs(path.Join(v.providerDir, "docs"))
err = v.validateStaticDocs(filepath.Join(v.providerDir, "docs"))
result = errors.Join(result, err)

}
if dirExists(path.Join(v.providerDir, "website/docs")) {
if dirExists(filepath.Join(v.providerDir, filepath.Join("website", "docs"))) {
v.logger.infof("detected legacy website directory, running checks")
err = v.validateLegacyWebsite(path.Join(v.providerDir, "website/docs"))
err = v.validateLegacyWebsite(filepath.Join(v.providerDir, "website/docs"))
result = errors.Join(result, err)
}

Expand Down Expand Up @@ -162,7 +161,7 @@ func (v *validator) validateStaticDocs(dir string) error {
return err
}
if d.IsDir() {
match, err := doublestar.Match(DocumentationDirGlobPattern, rel)
match, err := doublestar.PathMatch(filepath.FromSlash(DocumentationDirGlobPattern), rel)
if err != nil {
return err
}
Expand All @@ -174,7 +173,7 @@ func (v *validator) validateStaticDocs(dir string) error {
result = errors.Join(result, check.InvalidDirectoriesCheck(rel))
return nil
}
match, err := doublestar.Match(DocumentationGlobPattern, rel)
match, err := doublestar.PathMatch(filepath.FromSlash(DocumentationGlobPattern), rel)
if err != nil {
return err
}
Expand Down Expand Up @@ -246,7 +245,7 @@ func (v *validator) validateLegacyWebsite(dir string) error {
return err
}
if d.IsDir() {
match, err := doublestar.Match(DocumentationDirGlobPattern, rel)
match, err := doublestar.PathMatch(filepath.FromSlash(DocumentationDirGlobPattern), rel)
if err != nil {
return err
}
Expand All @@ -259,7 +258,7 @@ func (v *validator) validateLegacyWebsite(dir string) error {
return nil
}

match, err := doublestar.Match(DocumentationGlobPattern, rel)
match, err := doublestar.PathMatch(filepath.FromSlash(DocumentationGlobPattern), rel)
if err != nil {
return err
}
Expand Down
22 changes: 11 additions & 11 deletions internal/provider/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package provider

import (
"path"
"path/filepath"
"testing"

"github.com/bmatcuk/doublestar/v4"
Expand All @@ -15,13 +15,13 @@ func TestValidator_validate(t *testing.T) {
t.Parallel()

v := &validator{
providerDir: "testdata/valid-registry-directories",
providerDir: filepath.Join("testdata", "valid-registry-directories"),
providerName: "terraform-provider-null",

logger: NewLogger(cli.NewMockUi()),
}

err := v.validateStaticDocs(path.Join(v.providerDir, "docs"))
err := v.validateStaticDocs(filepath.Join(v.providerDir, "docs"))
if err != nil {
t.Fatalf("error retrieving schema: %q", err)
}
Expand All @@ -37,15 +37,15 @@ func TestValidateStaticDocs(t *testing.T) {
}{
{
Name: "valid registry directories",
BasePath: "testdata/valid-registry-directories",
BasePath: filepath.Join("testdata", "valid-registry-directories"),
},
{
Name: "valid registry directories with cdktf docs",
BasePath: "testdata/valid-registry-directories-with-cdktf",
BasePath: filepath.Join("testdata", "valid-registry-directories-with-cdktf"),
},
{
Name: "invalid registry directories",
BasePath: "testdata/invalid-registry-directories",
BasePath: filepath.Join("testdata", "invalid-registry-directories"),
ExpectError: true,
ExpectedError: "invalid Terraform Provider documentation directory found: docs/resources/invalid",
},
Expand All @@ -63,7 +63,7 @@ func TestValidateStaticDocs(t *testing.T) {
logger: NewLogger(cli.NewMockUi()),
}

got := v.validateStaticDocs(path.Join(v.providerDir, "docs"))
got := v.validateStaticDocs(filepath.Join(v.providerDir, "docs"))

if got == nil && testCase.ExpectError {
t.Errorf("expected error, got no error")
Expand All @@ -90,15 +90,15 @@ func TestValidateLegacyWebsite(t *testing.T) {
}{
{
Name: "valid legacy directories",
BasePath: "testdata/valid-legacy-directories",
BasePath: filepath.Join("testdata", "valid-legacy-directories"),
},
{
Name: "valid legacy directories with cdktf docs",
BasePath: "testdata/valid-legacy-directories-with-cdktf",
BasePath: filepath.Join("testdata", "valid-legacy-directories-with-cdktf"),
},
{
Name: "invalid legacy directories",
BasePath: "testdata/invalid-legacy-directories",
BasePath: filepath.Join("testdata", "invalid-legacy-directories"),
ExpectError: true,
ExpectedError: "invalid Terraform Provider documentation directory found: website/docs/r/invalid",
},
Expand All @@ -116,7 +116,7 @@ func TestValidateLegacyWebsite(t *testing.T) {
logger: NewLogger(cli.NewMockUi()),
}

got := v.validateLegacyWebsite(path.Join(v.providerDir, "website"))
got := v.validateLegacyWebsite(filepath.Join(v.providerDir, "website"))

if got == nil && testCase.ExpectError {
t.Errorf("expected error, got no error")
Expand Down

0 comments on commit 6fefacb

Please sign in to comment.