Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check multiple docs locations #1462

Merged
merged 3 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions pkg/tfgen/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,30 +390,6 @@ func overlayArgsToArgs(sourceDocs entityDocs, docs *entityDocs) {
docs.Arguments = docsArgs.collapse()
}

// 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)

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)
}

// Otherwise use the new location path.
kindString := string(kind)
return filepath.Join(repo, "docs", kindString)
}

//nolint:lll
var (
// For example:
Expand Down
68 changes: 53 additions & 15 deletions pkg/tfgen/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,23 +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 {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only change within this loop is the extra layer of indentation.

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
}

// getDocsPath finds the correct docs path for the repo/kind
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)
}

// ${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)
}

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"), 0600))
}

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)
})
}
}