Skip to content

Commit

Permalink
Allow numerical characters in example names/titles (#922)
Browse files Browse the repository at this point in the history
* Allow numerical characters in example names/titles

* Refine error message, add test case

* Update the documentation
  • Loading branch information
fbiville committed Mar 7, 2023
1 parent f565bd9 commit 2ec7297
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 13 deletions.
14 changes: 7 additions & 7 deletions docs/modules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ We have provided a command line tool to generate the scaffolding for the code of

### Command line flags

| Flag | Type | Required | Description |
|------|------|----------|-------------|
| -name | string | Yes | Name of the module, use camel-case when needed. Only alphabetical characters are allowed. |
| -image | string | Yes | Fully-qualified name of the Docker image to be used by the module (i.e. 'docker.io/org/project:tag') |
| -title | string | No | A variant of the name supporting mixed casing (i.e. 'MongoDB'). Only alphabetical characters are allowed. |
| -as-module | bool | No | If set, the module will be generated as a Go module, under the modules directory. Otherwise, it will be generated as a subdirectory of the examples directory. |
| Flag | Type | Required | Description |
|------------|--------|----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------|
| -name | string | Yes | Name of the module, use camel-case when needed. Only alphanumerical characters are allowed (leading character must be a letter). |
| -image | string | Yes | Fully-qualified name of the Docker image to be used by the module (i.e. 'docker.io/org/project:tag') |
| -title | string | No | A variant of the name supporting mixed casing (i.e. 'MongoDB'). Only alphanumerical characters are allowed (leading character must be a letter). |
| -as-module | bool | No | If set, the module will be generated as a Go module, under the modules directory. Otherwise, it will be generated as a subdirectory of the examples directory. |

### What is this tool not doing?

- If the module name does not contain alphabetical characters, it will exit the generation.
- If the module name or title does not contain alphanumerical characters, it will exit the generation.
- If the module already exists, it will exit without updating the existing files.

### How to run the tool
Expand Down
8 changes: 4 additions & 4 deletions modulegen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ func (e *Example) Type() string {
}

func (e *Example) Validate() error {
if !regexp.MustCompile(`^[A-Za-z]+$`).MatchString(e.Name) {
return fmt.Errorf("invalid name: %s. Only alphabetical characters are allowed", e.Name)
if !regexp.MustCompile(`^[A-Za-z][A-Za-z0-9]*$`).MatchString(e.Name) {
return fmt.Errorf("invalid name: %s. Only alphanumerical characters are allowed (leading character must be a letter)", e.Name)
}

if !regexp.MustCompile(`^[A-Za-z]+$`).MatchString(e.TitleName) {
return fmt.Errorf("invalid title: %s. Only alphabetical characters are allowed", e.TitleName)
if !regexp.MustCompile(`^[A-Za-z][A-Za-z0-9]*$`).MatchString(e.TitleName) {
return fmt.Errorf("invalid title: %s. Only alphanumerical characters are allowed (leading character must be a letter)", e.TitleName)
}

return nil
Expand Down
75 changes: 73 additions & 2 deletions modulegen/main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -77,6 +78,76 @@ func TestExample(t *testing.T) {
}
}

func TestExample_Validate(outer *testing.T) {
outer.Parallel()

tests := []struct {
name string
example Example
expectedErr error
}{
{
name: "only alphabetical characters in name/title",
example: Example{
Name: "AmazingDB",
TitleName: "AmazingDB",
},
},
{
name: "alphanumerical characters in name",
example: Example{
Name: "AmazingDB4tw",
TitleName: "AmazingDB",
},
},
{
name: "alphanumerical characters in title",
example: Example{
Name: "AmazingDB",
TitleName: "AmazingDB4tw",
},
},
{
name: "non-alphanumerical characters in name",
example: Example{
Name: "Amazing DB 4 The Win",
TitleName: "AmazingDB",
},
expectedErr: errors.New("invalid name: Amazing DB 4 The Win. Only alphanumerical characters are allowed (leading character must be a letter)"),
},
{
name: "non-alphanumerical characters in title",
example: Example{
Name: "AmazingDB",
TitleName: "Amazing DB 4 The Win",
},
expectedErr: errors.New("invalid title: Amazing DB 4 The Win. Only alphanumerical characters are allowed (leading character must be a letter)"),
},
{
name: "leading numerical character in name",
example: Example{
Name: "1AmazingDB",
TitleName: "AmazingDB",
},
expectedErr: errors.New("invalid name: 1AmazingDB. Only alphanumerical characters are allowed (leading character must be a letter)"),
},
{
name: "leading numerical character in title",
example: Example{
Name: "AmazingDB",
TitleName: "1AmazingDB",
},
expectedErr: errors.New("invalid title: 1AmazingDB. Only alphanumerical characters are allowed (leading character must be a letter)"),
},
}

for _, test := range tests {
outer.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.expectedErr, test.example.Validate())
})
}
}

func TestGenerateWrongExampleName(t *testing.T) {
rootTmp := t.TempDir()
examplesTmp := filepath.Join(rootTmp, "examples")
Expand Down Expand Up @@ -190,8 +261,8 @@ func TestGenerate(t *testing.T) {
assert.Nil(t, err)

example := Example{
Name: "foodb",
TitleName: "FooDB",
Name: "foodb4tw",
TitleName: "FooDB4TheWin",
IsModule: false,
Image: "docker.io/example/foodb:latest",
TCVersion: "v0.0.0-test",
Expand Down

0 comments on commit 2ec7297

Please sign in to comment.