Skip to content

Commit

Permalink
Check all valid locations
Browse files Browse the repository at this point in the history
  • Loading branch information
iwahbe committed Oct 23, 2023
1 parent 7ca2c54 commit 7752ad8
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 33 deletions.
80 changes: 47 additions & 33 deletions pkg/tfgen/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,47 +193,61 @@ func getMarkdownNames(packagePrefix, rawName string, globalInfo *tfbridge.DocRul

// readMarkdown searches all possible locations for the markdown content
func readMarkdown(repo string, kind DocKind, possibleLocations []string) (*DocFile, error) {
locationPrefix := getDocsPath(repo, kind)
locationPrefix, err := getDocsPath(repo, kind)
if err != nil {
return nil, fmt.Errorf("could not gather location prefix for %q: %w", repo, err)
}

for _, name := range possibleLocations {
location := filepath.Join(locationPrefix, name)
markdownBytes, err := os.ReadFile(location)
if err == nil {
return &DocFile{markdownBytes, name}, nil
} else if !os.IsNotExist(err) && !errors.Is(err, &os.PathError{}) {
// Missing doc files are expected and OK.
//
// If the file we expect is actually a directory (PathError), that
// is also OK.
//
// Other errors (such as permission errors) indicate a problem
// with the host system, and should be reported.
return nil, fmt.Errorf("%s: %w", location, err)
for _, prefix := range locationPrefix {
for _, name := range possibleLocations {
location := filepath.Join(prefix, name)
markdownBytes, err := os.ReadFile(location)
if err == nil {
return &DocFile{markdownBytes, name}, nil
} else if !os.IsNotExist(err) && !errors.Is(err, &os.PathError{}) {
// Missing doc files are expected and OK.
//
// If the file we expect is actually a directory (PathError), that
// is also OK.
//
// Other errors (such as permission errors) indicate a problem
// with the host system, and should be reported.
return nil, fmt.Errorf("%s: %w", location, err)
}
}
}
return nil, nil
}

// checkIfNewDocsExist checks if the new docs root exists
func checkIfNewDocsExist(repo string) bool {
// Check if the new docs path exists
newDocsPath := filepath.Join(repo, "docs", "resources")
_, err := os.Stat(newDocsPath)
return !os.IsNotExist(err)
}

// getDocsPath finds the correct docs path for the repo/kind
func getDocsPath(repo string, kind DocKind) string {
// Check if the new docs path exists
newDocsExist := checkIfNewDocsExist(repo)
func getDocsPath(repo string, kind DocKind) ([]string, error) {
var err error
exists := func(p string) bool {
_, sErr := os.Stat(p)
if sErr == nil {
return true
} else if os.IsNotExist(sErr) {
return false
}
err = sErr
return false
}

var paths []string

// ${repo}/docs/resources
//
// This is TF's new and preferred way to describe docs.
if p := filepath.Join(repo, "docs", string(kind)); exists(p) {
paths = append(paths, p)
}

if !newDocsExist {
// If the new path doesn't exist, use the old docs path.
kindString := string([]rune(kind)[0]) // We only want the first letter because the old path uses "r" and "d"
return filepath.Join(repo, "website", "docs", kindString)
// ${repo}/website/docs/r
//
// This is the legacy way to describe docs.
if p := filepath.Join(repo, "website", "docs", string(kind)[:1]); exists(p) {
paths = append(paths, p)
}

// Otherwise use the new location path.
kindString := string(kind)
return filepath.Join(repo, "docs", kindString)
return paths, err
}
93 changes: 93 additions & 0 deletions pkg/tfgen/source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright 2016-2023, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package tfgen

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

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

func TestGetDocsPath(t *testing.T) {
t.Parallel()
tests := []struct {
files []string
expectedResource []string
expectedDataSource []string
}{
{files: []string{"main.go"}}, // No docs files => no valid paths

{
files: []string{
filepath.Join("website", "docs", "r", "foo.md"),
filepath.Join("website", "docs", "d", "bar.md"),
},
expectedResource: []string{filepath.Join("website", "docs", "r")},
expectedDataSource: []string{filepath.Join("website", "docs", "d")},
},
{
files: []string{
filepath.Join("docs", "resources", "foo.md"),
filepath.Join("website", "docs", "d", "bar.md"),
},
expectedResource: []string{filepath.Join("docs", "resources")},
expectedDataSource: []string{filepath.Join("website", "docs", "d")},
},
{
files: []string{
filepath.Join("docs", "resources", "foo.md"),
filepath.Join("website", "docs", "r", "bar.md"),
},
expectedResource: []string{
filepath.Join("docs", "resources"),
filepath.Join("website", "docs", "r"),
},
},
}

for _, tt := range tests {
tt := tt
t.Run("", func(t *testing.T) {
t.Parallel()

repo := t.TempDir()
for _, f := range tt.files {
p := filepath.Join(repo, f)
require.NoError(t, os.MkdirAll(filepath.Dir(p), 0700))
require.NoError(t, os.WriteFile(p, []byte("test"), 0700))

Check failure on line 72 in pkg/tfgen/source_test.go

View workflow job for this annotation

GitHub Actions / Build and Test Bridge (1.20.x, macos-latest)

G306: Expect WriteFile permissions to be 0600 or less (gosec)
}

check := func(expected, actual []string, err error) {
if !assert.NoError(t, err) {
return
}
for i, v := range expected {
expected[i] = filepath.Join(repo, v)
}
assert.Equal(t, expected, actual)

}

actualResource, err := getDocsPath(repo, ResourceDocs)
check(tt.expectedResource, actualResource, err)

actualDataSource, err := getDocsPath(repo, DataSourceDocs)
check(tt.expectedDataSource, actualDataSource, err)
})
}
}

0 comments on commit 7752ad8

Please sign in to comment.