Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
add more tests for render
  • Loading branch information
austinvalle committed Jun 26, 2025
commit 4619a794705777144990e10f13e62dbd0d739cea
3 changes: 0 additions & 3 deletions internal/schemamd/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ func writeIdentitySchemaAttributes(w io.Writer, attrs map[string]*tfjson.Identit

for i, name := range requiredForImportNames {
requiredAttr := attrs[name]
// Write the header
if i == 0 {
_, err := io.WriteString(w, "#### Required\n\n")
if err != nil {
Expand All @@ -100,7 +99,6 @@ func writeIdentitySchemaAttributes(w io.Writer, attrs map[string]*tfjson.Identit

for i, name := range optionalForImportNames {
optionalAttr := attrs[name]
// Write the header
if i == 0 {
_, err := io.WriteString(w, "#### Optional\n\n")
if err != nil {
Expand All @@ -117,7 +115,6 @@ func writeIdentitySchemaAttributes(w io.Writer, attrs map[string]*tfjson.Identit
return nil
}

// TODO: I think this is enough, no need for nested types of anything complicated because the schema is simple.
func writeIdentityAttribute(w io.Writer, name string, attr *tfjson.IdentityAttribute) error {
_, err := io.WriteString(w, "- `"+name+"` ")
if err != nil {
Expand Down
53 changes: 53 additions & 0 deletions internal/schemamd/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,56 @@ func TestRender(t *testing.T) {
})
}
}

func TestRenderIdentitySchema(t *testing.T) {
t.Parallel()

for _, c := range []struct {
name string
inputFile string
expectedFile string
}{
{
"identity",
"testdata/identity.schema.json",
"testdata/identity.md",
},
} {
t.Run(c.name, func(t *testing.T) {
t.Parallel()

input, err := os.ReadFile(c.inputFile)
if err != nil {
t.Fatal(err)
}

expected, err := os.ReadFile(c.expectedFile)
if err != nil {
t.Fatal(err)
}

var identitySchema tfjson.IdentitySchema

err = json.Unmarshal(input, &identitySchema)
if err != nil {
t.Fatal(err)
}

b := &strings.Builder{}
err = schemamd.RenderIdentitySchema(&identitySchema, b)
if err != nil {
t.Fatal(err)
}

// Remove \r characters so tests don't fail on windows
expectedStr := strings.ReplaceAll(string(expected), "\r", "")

// Remove trailing newlines before comparing (some text editors remove them).
expectedStr = strings.TrimRight(expectedStr, "\n")
actual := strings.TrimRight(b.String(), "\n")
if diff := cmp.Diff(expectedStr, actual); diff != "" {
t.Fatalf("Unexpected diff (-wanted, +got): %s", diff)
}
})
}
}
14 changes: 14 additions & 0 deletions internal/schemamd/testdata/identity.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
### Identity Schema

#### Required

- `bool_attribute` (Boolean) example bool attribute
- `list_of_bools_attribute` (List of Boolean) example list attribute with bool elements
- `list_of_strings_attribute` (List of String) example list attribute with string elements
- `string_attribute` (String) example string attribute

#### Optional

- `float64_attribute` (Number) example float64 attribute
- `int64_attribute` (Number) example int64 attribute
- `number_attribute` (Number) example number attribute
46 changes: 46 additions & 0 deletions internal/schemamd/testdata/identity.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"version": 0,
"attributes": {
"float64_attribute": {
"type": "number",
"description": "example float64 attribute",
"optional_for_import": true
},
"number_attribute": {
"type": "number",
"description": "example number attribute",
"optional_for_import": true
},
"list_of_strings_attribute": {
"type": [
"list",
"string"
],
"description": "example list attribute with string elements",
"required_for_import": true
},
"bool_attribute": {
"type": "bool",
"description": "example bool attribute",
"required_for_import": true
},
"string_attribute": {
"type": "string",
"description": "example string attribute",
"required_for_import": true
},
"int64_attribute": {
"type": "number",
"description": "example int64 attribute",
"optional_for_import": true
},
"list_of_bools_attribute": {
"type": [
"list",
"bool"
],
"description": "example list attribute with bool elements",
"required_for_import": true
}
}
}