Skip to content

Commit

Permalink
Merge pull request open-telemetry#77 from jjackson-s/testCompwiz
Browse files Browse the repository at this point in the history
Test compwiz
  • Loading branch information
pmcollins authored Jul 19, 2021
2 parents 4b5b18c + 2b314e6 commit dacff34
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 7 deletions.
14 changes: 7 additions & 7 deletions cmd/configschema/configwiz/configwiz/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,32 +56,32 @@ func handleComponent(
) {
typeMap := map[string]interface{}{}
m[componentGroup+"s"] = typeMap
io := clio{printLine, readline}
for _, name := range names {
cfgInfo, err := configschema.GetCfgInfo(factories, componentGroup, strings.Split(name, "/")[0])
if err != nil {
panic(err)
}
fmt.Printf("%s %q\n", strings.Title(componentGroup), name)
f := configschema.ReadFields(reflect.ValueOf(cfgInfo.CfgInstance), dr)
typeMap[name] = componentWizard(0, f)
typeMap[name] = componentWizard(io, 0, f)
}
}

func componentWizard(lvl int, f *configschema.Field) map[string]interface{} {
func componentWizard(io clio, lvl int, f *configschema.Field) map[string]interface{} {
out := map[string]interface{}{}
io := clio{printLine, readline}
p := io.newIndentingPrinter(lvl)
for _, field := range f.Fields {
if field.Name == "squash" {
componentWizard(lvl, field)
componentWizard(io, lvl, field)
} else if field.Kind == "struct" {
p.println(field.Name)
out[field.Name] = componentWizard(lvl+1, field)
out[field.Name] = componentWizard(io, lvl+1, field)
} else if field.Kind == "ptr" {
p.print(fmt.Sprintf("%s (optional) skip (Y/n)> ", field.Name))
in := readline("")
in := io.read("")
if in == "n" {
out[field.Name] = componentWizard(lvl+1, field)
out[field.Name] = componentWizard(io, lvl+1, field)
}
} else {
handleField(io, p, field, out)
Expand Down
64 changes: 64 additions & 0 deletions cmd/configschema/configwiz/configwiz/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ func buildExpectedOutput(indent int, prefix string, name string, typ string, def
}
if defaultStr {
prefix += tab + "Default (enter to accept): defaultStr1\n"
} else {
prefix += tab + "Default (enter to accept): defaultStr\n"
}
prefix += tab + "> "
return prefix
Expand All @@ -81,6 +83,68 @@ func buildTestCFGFields(name string, typ string, kind string, defaultStr string,
return cfgField
}

func runCompWizard(io clio, name string, typ string, kind string, defaultStr string, doc string) configschema.Field {
const test = "test"
cfgField := buildTestCFGFields(
name+test,
typ+test,
kind+test,
"defaultStr",
doc+test,
)
newField := buildTestCFGFields(
name,
test+typ,
kind,
"defaultStr",
test+doc,
)
var fields []*configschema.Field
fields = append(fields, &newField)
cfgField.Fields = fields
componentWizard(io, 0, &cfgField)
return cfgField
}

func TestComponentWizardSquash(t *testing.T) {
writerSquash := fakeWriter{}
ioSquash := clio{writerSquash.write, fakeReader{}.read}
cfgSquash := runCompWizard(ioSquash, "squash", "test", "helper", "", "testing compWizardSquash")
squash := cfgSquash.Fields[0].Fields[0]
expectedSquash := buildExpectedOutput(0, "", squash.Name, squash.Type, true, squash.Doc)
assert.Equal(t, expectedSquash, writerSquash.programOutput)
}

func TestComponentWizardStruct(t *testing.T) {
writerStruct := fakeWriter{}
ioStruct := clio{writerStruct.write, fakeReader{}.read}
cfgStruct := runCompWizard(ioStruct, "struct", "test", "struct", "", "testing CompWizard Struct")
struc := cfgStruct.Fields[0].Fields[0]
expectedStruct := fmt.Sprintf("%s\n", cfgStruct.Fields[0].Name)
expectedStruct = buildExpectedOutput(1, expectedStruct, struc.Name, struc.Type, true, struc.Doc)
assert.Equal(t, expectedStruct, writerStruct.programOutput)

}

func TestComponentWizardPtr(t *testing.T) {
writerPtr := fakeWriter{}
ioPtr := clio{writerPtr.write, fakeReader{"n"}.read}
cfgPtr := runCompWizard(ioPtr, "ptr", "test", "ptr", "", "testing CompWizard ptr")
ptr := cfgPtr.Fields[0].Fields[0]
expectedPtr := fmt.Sprintf("%s (optional) skip (Y/n)> ", string(cfgPtr.Fields[0].Name))
expectedPtr = buildExpectedOutput(1, expectedPtr, ptr.Name, ptr.Type, true, ptr.Doc)
assert.Equal(t, expectedPtr, writerPtr.programOutput)
}

func TestComponentWizardHandle(t *testing.T) {
writerHandle := fakeWriter{}
ioHandle := clio{writerHandle.write, fakeReader{}.read}
cfgHandle := runCompWizard(ioHandle, "handle", "test", "helper", "", "testing CompWizard handle")
field := cfgHandle.Fields[0]
expectedHandle := buildExpectedOutput(0, "", field.Name, field.Type, false, field.Doc)
assert.Equal(t, expectedHandle, writerHandle.programOutput)
}

func TestHandleField(t *testing.T) {
writer := fakeWriter{}
reader := fakeReader{}
Expand Down

0 comments on commit dacff34

Please sign in to comment.