Skip to content

Commit

Permalink
updated README and added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Skarlso committed May 23, 2024
1 parent 6bd2917 commit 4c2bad9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,27 @@ To get an HTML output provide the format flag like this:
cty generate -c delivery.krok.app_krokcommands --comments --format html
```

### Minimal required CRD sample

It's possible to generate a sample YAML for a CRD that will make the CRD validation pass. Meaning, it will only contain
samples for fields that are actually required. All other fields will be ignored.

For example, a CRD having a single required field with an example and the rest being optional would generate something
like this:

```yaml
apiVersion: delivery.krok.app/v1alpha1
kind: KrokCommand
spec:
image: "krok-hook/slack-notification:v0.0.1"
```

To run cty with minimal required fields, pass in `--minimal` to the command like this:

```
cty generate -c delivery.krok.app_krokcommands --comments --minimal --format html
```

## WASM frontend

There is a WASM based frontend that can be started by navigating into the `wasm` folder and running the following make
Expand Down
20 changes: 20 additions & 0 deletions pkg/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,23 @@ func TestGenerateMinimal(t *testing.T) {

assert.Equal(t, golden, buffer.Bytes())
}

func TestGenerateMinimalWithExample(t *testing.T) {
content, err := os.ReadFile(filepath.Join("testdata", "sample_crd_with_example.yaml"))
require.NoError(t, err)

crd := &v1beta1.CustomResourceDefinition{}
require.NoError(t, yaml.Unmarshal(content, crd))

var output []byte
buffer := bytes.NewBuffer(output)

parser := NewParser(crd.Spec.Group, crd.Spec.Names.Kind, false, true)
version := crd.Spec.Versions[0]
require.NoError(t, parser.ParseProperties(version.Name, buffer, version.Schema.OpenAPIV3Schema.Properties, RootRequiredFields))

golden, err := os.ReadFile(filepath.Join("testdata", "sample_crd_with_minimal_example_with_example_for_field_golden.yaml"))
require.NoError(t, err)

assert.Equal(t, golden, buffer.Bytes())
}
4 changes: 2 additions & 2 deletions pkg/testdata/sample_crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ spec:
description: Enabled defines if this command can be executed or not.
type: boolean
image:
description: 'Image defines the image name and tag of the command
example: krok-hook/slack-notification:v0.0.1'
description: 'Image defines the image name and tag of the command.
example: krok-hook/slack-notification:v0.0.1'
type: string
platforms:
description: Platforms holds all the platforms which this command
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: delivery.krok.app/v1alpha1
kind: KrokCommand
spec:
image: "krok-hook/slack-notification:v0.0.1"

0 comments on commit 4c2bad9

Please sign in to comment.