-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: facilitate testing scaffolds (#123)
* simplify implementation * add test mode to scaffold new command * implement test example in ci * change hello to cli * fix cli * use proper version * duhhhh * pull out test into file * docs for testing * rework tests to support cases and AST output * fix output example * re-org commands folder * guard against out of bounds access * revert back to new command with more generic implementation * ignore file close err * simplify test script for cli * fix taskfile test * basic testing scaffold docs * rework script tests * change ci runner * fix and add tests for setting 'Project' var * nested output tests * reorder task files
- Loading branch information
Showing
38 changed files
with
784 additions
and
425 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
questions: | ||
- name: "question_1" | ||
prompt: | ||
message: "Question 1" | ||
required: true | ||
- name: "question_2" | ||
prompt: | ||
message: "Question 2" | ||
required: true | ||
- name: "question_3" | ||
prompt: | ||
message: "Question 3" | ||
required: true | ||
- name: "question_4" | ||
prompt: | ||
message: "Question 4" | ||
required: true | ||
|
||
presets: | ||
default: | ||
Project: "nested-defaults" | ||
question_1: "Answer 1" | ||
question_2: "Answer 2" | ||
question_3: "Answer 3" | ||
question_4: "Answer 4" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{ .Scaffold.question_2 }} |
1 change: 1 addition & 0 deletions
1
.examples/nested/{{ .ProjectKebab }}/child/subchild/child_2.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{ .Scaffold.question_3 }} |
1 change: 1 addition & 0 deletions
1
.examples/nested/{{ .ProjectKebab }}/child/subchild/subsubchild/child_3.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{ .Scaffold.question_4 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{{ .Scaffold.question_1 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
package commands | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"os" | ||
"strings" | ||
|
||
"github.com/go-git/go-git/v5/plumbing/transport" | ||
"github.com/go-git/go-git/v5/plumbing/transport/http" | ||
"github.com/hay-kot/scaffold/app/core/fsast" | ||
"github.com/hay-kot/scaffold/app/scaffold" | ||
"github.com/hay-kot/scaffold/app/scaffold/pkgs" | ||
"github.com/sahilm/fuzzy" | ||
) | ||
|
||
type FlagsNew struct { | ||
NoPrompt bool | ||
Preset string | ||
Snapshot string | ||
} | ||
|
||
func (ctrl *Controller) New(args []string, flags FlagsNew) error { | ||
if len(args) == 0 { | ||
return fmt.Errorf("missing scaffold name") | ||
} | ||
|
||
path, err := ctrl.resolve(args[0], flags.NoPrompt) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if path == "" { | ||
return fmt.Errorf("missing scaffold path") | ||
} | ||
|
||
rest := args[1:] | ||
argvars, err := parseArgVars(rest) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var varfunc func(*scaffold.Project) (map[string]any, error) | ||
switch { | ||
case flags.NoPrompt: | ||
varfunc = func(p *scaffold.Project) (map[string]any, error) { | ||
caseVars, ok := p.Conf.Presets[flags.Preset] | ||
if !ok { | ||
return nil, fmt.Errorf("case %s not found", flags.Preset) | ||
} | ||
|
||
project, ok := caseVars["Project"].(string) | ||
if !ok || project == "" { | ||
// Generate 4 random digits | ||
name := fmt.Sprintf("scaffold-test-%04d", rand.Intn(10000)) | ||
caseVars["Project"] = name | ||
project = name | ||
} | ||
p.Name = project | ||
|
||
// Test cases do not use rc.Defaults | ||
vars := scaffold.MergeMaps(caseVars, argvars) | ||
return vars, nil | ||
} | ||
|
||
default: | ||
varfunc = func(p *scaffold.Project) (map[string]any, error) { | ||
vars := scaffold.MergeMaps(argvars, ctrl.rc.Defaults) | ||
vars, err = p.AskQuestions(vars, ctrl.engine) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return vars, nil | ||
} | ||
} | ||
|
||
outfs := ctrl.Flags.OutputFS() | ||
|
||
err = ctrl.runscaffold(runconf{ | ||
scaffolddir: path, | ||
showMessages: !flags.NoPrompt, | ||
varfunc: varfunc, | ||
outputfs: outfs, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if flags.Snapshot != "" { | ||
ast, err := fsast.New(outfs) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if flags.Snapshot == "stdout" { | ||
fmt.Println(ast.String()) | ||
} else { | ||
file, err := os.Create(flags.Snapshot) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_ = file.Close() | ||
|
||
_, err = file.WriteString(ast.String()) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (ctrl *Controller) fuzzyFallBack(str string) ([]string, []string, error) { | ||
systemScaffolds, err := pkgs.ListSystem(os.DirFS(ctrl.Flags.Cache)) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
localScaffolds, err := pkgs.ListLocal(os.DirFS(ctrl.Flags.OutputDir)) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
systemMatches := fuzzy.Find(str, systemScaffolds) | ||
systemMatchesOutput := make([]string, len(systemMatches)) | ||
for i, match := range systemMatches { | ||
systemMatchesOutput[i] = match.Str | ||
} | ||
|
||
localMatches := fuzzy.Find(str, localScaffolds) | ||
localMatchesOutput := make([]string, len(localMatches)) | ||
for i, match := range localMatches { | ||
localMatchesOutput[i] = match.Str | ||
} | ||
|
||
return systemMatchesOutput, localMatchesOutput, nil | ||
} | ||
|
||
func basicAuthAuthorizer(pkgurl, username, password string) pkgs.AuthProviderFunc { | ||
return func(url string) (transport.AuthMethod, bool) { | ||
if url != pkgurl { | ||
return nil, false | ||
} | ||
|
||
return &http.BasicAuth{ | ||
Username: username, | ||
Password: password, | ||
}, true | ||
} | ||
} | ||
|
||
func parseArgVars(args []string) (map[string]any, error) { | ||
vars := make(map[string]any, len(args)) | ||
|
||
for _, v := range args { | ||
if !strings.Contains(v, "=") { | ||
return nil, fmt.Errorf("variable %s is not in the form of key=value", v) | ||
} | ||
|
||
kv := strings.Split(v, "=") | ||
vars[kv[0]] = kv[1] | ||
} | ||
|
||
return vars, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.