hay-kot / scaffold

A cookie cutter alternative with in-project scaffolding for generating components, controllers, or other common code patterns.
https://hay-kot.github.io/scaffold/
MIT License
64 stars 8 forks source link

Guidance on how to test scaffolds #122

Closed alexeagle closed 6 months ago

alexeagle commented 6 months ago

I'd like to know that the content of the scaffold produces a working output, by writing a simple automation to call scaffold, then cd into the new folder and run something on the code inside.

I tried several of the techniques on https://www.baeldung.com/linux/bash-interactive-prompts to supply prompt answers over stdin, but have errors like:

% yes | scaffold new $PWD

   starter                                                             

  Generate boilerplate code for a new project

? Project name panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x0 pc=0x10095a128]

goroutine 1 [running]:
github.com/AlecAivazis/survey/v2/terminal.(*RuneReader).ReadLineWithDefault.func2(...)
        /home/runner/go/pkg/mod/github.com/!alec!aivazis/survey/v2@v2.3.7/terminal/runereader.go:65
github.com/AlecAivazis/survey/v2/terminal.(*RuneReader).ReadLineWithDefault(0x140001439e0, 0x0, {0x0, 0x0, 0x0}, {0x14000680f20?, 0x14000680e48?, 0x1004f0e08?})

Alternatively I suppose you could have a first-class support for testing by allowing the scaffold CLI to accept a "script" with expected prompts and supplied answers.

hay-kot commented 6 months ago

You can supply arguments after the path, we do some "basic" logic to turn those arguments into variables

See

https://github.com/hay-kot/scaffold/blob/f5e748941ecc2aa324ec4f23ab70003554149d72/app/commands/new.go#L177-L183

This isn't something I thought a ton about so I'm happy to consider alternative APIs and/or fix issues with this approach.

hay-kot commented 6 months ago

Okay, I did some more digging and it looks like this isn't possible, you will always be prompted. I see two options here

Option 1

Add --no-interaction flag that skips Q/A and just use the arguments like I show above

Option 2

Add a --test flag and introduce a test top level property into the scaffold file that will be used as variables for test

test:
  Var1: value 1
  Var2: value 2

I'm more keen on option 2, but I'm open to any suggestions you have