func TestTestRulesApplyToObjects(t *testing.T) { cb := tests.New().TempGopath(true).CopyToTemp("kego.io/process/validate/tests") defer cb.Cleanup() path, dir := cb.TempPackage("a", map[string]string{ "a.yml": ` type: system:package id: a aliases: tests: kego.io/process/validate/tests `, "b.yml": ` type: tests:a id: b b: foobar rules: - selector: ".b" type: "system:@string" max-length: 5 `, }) cb.Path(path).Dir(dir).Alias("tests", "kego.io/process/validate/tests").Jauto().Sauto(parser.Parse) errors, err := ValidatePackage(cb.Ctx()) require.NoError(t, err) assert.IsError(t, errors[0], "HLKQWDCMRN") }
func TestRulesEnforcer(t *testing.T) { cb := tests.New().TempGopath(true).CopyToTemp("kego.io/process/validate/tests") defer cb.Cleanup() path, dir := cb.TempPackage("a", map[string]string{ "a.yml": ` type: system:package id: a aliases: tests: kego.io/process/validate/tests `, "b.yml": ` type: tests:a id: b b: foo rules: - type: tests:@a `, }) cb.Path(path).Dir(dir).Alias("tests", "kego.io/process/validate/tests").Jauto().Sauto(parser.Parse) errors, err := ValidatePackage(cb.Ctx()) require.NoError(t, err) assert.Equal(t, 0, len(errors)) }
func TestRuleId(t *testing.T) { cb := tests.New().TempGopath(true) defer cb.Cleanup() pathA, dirA := cb.TempPackage("a", map[string]string{ "a.json": `{ "type": "system:type", "id": "a", "rule": { "type": "system:type", "id": "foo" } }`, }) cb.Path(pathA).Dir(dirA).Cmd().Sempty().Jsystem() _, err := Parse(cb.Ctx(), pathA) assert.IsError(t, err, "VFUNPHUFHD") assert.HasError(t, err, "JKARKEDTIW") cb.TempFile("a.json", `{ "type": "system:type", "id": "a", "rule": { "type": "system:type" } }`) _, err = Parse(cb.Ctx(), pathA) assert.IsError(t, err, "VFUNPHUFHD") assert.HasError(t, err, "LMALEMKFDI") }
func TestStructs(t *testing.T) { cb := tests.New().TempGopath(true) defer cb.Cleanup() testTypeCollection(t, cb) testTypeAlias(t, cb) }
func TestImportSystem(t *testing.T) { cb := tests.New().TempGopath(true) defer cb.Cleanup() pathA, dirA := cb.TempPackage("a", map[string]string{ "package.json": `{ "type": "system:package", "aliases": { "s": "kego.io/system" } }`, }) cb.Path(pathA).Dir(dirA).Cmd().Sempty().Jsystem() _, err := Parse(cb.Ctx(), pathA) assert.IsError(t, err, "EWMLNJDXKC") pathB, dirB := cb.TempPackage("b", map[string]string{ "package.json": `{ "type": "system:package", "aliases": { "system": "a.b/c" } }`, }) cb.Path(pathB).Dir(dirB) _, err = Parse(cb.Ctx(), pathB) assert.IsError(t, err, "EWMLNJDXKC") }
func TestImport(t *testing.T) { cb := tests.New().TempGopath(true) defer cb.Cleanup() pathA, _ := cb.TempPackage("a", map[string]string{ "a.json": `{ "type": "system:type", "id": "a" }`, }) pathB, dirB := cb.TempPackage("b", map[string]string{ "package.json": `{ "type": "system:package", "aliases": { "a": "` + pathA + `" } }`, "b.json": `{ "type": "a:a", "id": "a" }`, "c.json": `{ "type": "system:foo", "id": "c" }`, }) cb.Path(pathB).Dir(dirB).Cmd().Sempty().Jsystem() _, err := Parse(cb.Ctx(), pathB) require.NoError(t, err) }
func TestParse1(t *testing.T) { cb := tests.New().TempGopath(true) defer cb.Cleanup() path, dir := cb.TempPackage("a", map[string]string{ "a.json": `{ "description": "a", "type": "system:type", "id": "b", "fields": { "c": { "type": "system:@string" } } }`, }) cb.Path(path).Dir(dir).Cmd().Sempty().Jsystem() _, err := Parse(cb.Ctx(), path) require.NoError(t, err) scache := sysctx.FromContext(cb.Ctx()) i, ok := scache.GetType(path, "b") assert.True(t, ok) ty, ok := i.(*system.Type) assert.True(t, ok) assert.Equal(t, "a", ty.Description) }
func TestValidate_NeedsTypes(t *testing.T) { cb := tests.New().TempGopath(true) defer cb.Cleanup() path, dir := cb.TempPackage("a", map[string]string{ "a.json": `{ "description": "a", "type": "system:type", "id": "a", "fields": { "a": { "type": "system:@string" } } }`, }) cb.Path(path).Dir(dir).Jsystem().Sauto(parser.Parse) errors, err := ValidatePackage(cb.Ctx()) require.NoError(t, err) assert.Equal(t, 0, len(errors)) }
func TestScanDirToFiles(t *testing.T) { cb := tests.New().TempGopath(false) defer cb.Cleanup() _, dir := cb.TempPackage("a", map[string]string{ "b.json": "c", }) // make a dir so we hit the IsRegular block require.NoError(t, os.Mkdir(filepath.Join(dir, "d"), 0777)) // put another file in it require.NoError(t, ioutil.WriteFile(filepath.Join(dir, "d", "e.json"), []byte("f"), 0777)) ch := ScanDirToFiles(cb.Ctx(), dir, false) out := []File{} for f := range ch { require.NoError(t, f.Err) out = append(out, f) } assert.Equal(t, 1, len(out)) assert.Equal(t, filepath.Join(dir, "b.json"), out[0].File) ch = ScanDirToFiles(cb.Ctx(), dir, true) out = []File{} for f := range ch { require.NoError(t, f.Err) out = append(out, f) } assert.Equal(t, 2, len(out)) assert.Equal(t, filepath.Join(dir, "b.json"), out[0].File) assert.Equal(t, filepath.Join(dir, "d", "e.json"), out[1].File) }
func testGenerated(t *testing.T, path string) { cb := tests.New().Path(path).Jauto().Sauto(parser.Parse) pi, err := parser.Parse(cb.Ctx(), path) require.NoError(t, err) generatedBytes, err := generate.Structs(cb.Ctx(), pi.Env) require.NoError(t, err) generatedString := string(generatedBytes) existingFilePath := filepath.Join(pi.Dir, "generated.go") existingBytes, err := ioutil.ReadFile(existingFilePath) require.NoError(t, err) existingString := string(existingBytes) // TODO: The "goimports" tool will often re-order the imports, so this is // a kludge to remove it before comparing. This is not ideal! importsRegex := regexp.MustCompile(`(?ms:\nimport \(\n.*\n\)\n)`) generatedString = importsRegex.ReplaceAllString(generatedString, "-") existingString = importsRegex.ReplaceAllString(existingString, "-") if generatedString != existingString { fmt.Println("Generated code for " + path + " is not what is present:") fmt.Println(generatedString) } require.Equal(t, generatedString, existingString) }
func TestFieldExtraRulesObject(t *testing.T) { cb := tests.New().TempGopath(true).CopyToTemp("kego.io/process/validate/tests") defer cb.Cleanup() path, dir := cb.TempPackage("a", map[string]string{ "a.yml": ` type: system:package id: a aliases: tests: kego.io/process/validate/tests `, "b.yml": ` type: tests:f id: b a: type: tests:a b: foo `, }) cb.Path(path).Dir(dir).Alias("tests", "kego.io/process/validate/tests").Jauto().Sauto(parser.Parse) errors, err := ValidatePackage(cb.Ctx()) require.NoError(t, err) assert.IsError(t, errors[0], "HLKQWDCMRN") }
func TestRuleHasExtraRules(t *testing.T) { cb := tests.New().TempGopath(true).CopyToTemp("kego.io/process/validate/tests") defer cb.Cleanup() path, dir := cb.TempPackage("a", map[string]string{ "a.yml": ` type: system:package id: a aliases: tests: kego.io/process/validate/tests `, "b.yml": ` type: tests:f id: b d: foo `, }) cb.Path(path).Dir(dir).Alias("tests", "kego.io/process/validate/tests").Jauto().Sauto(parser.Parse) errors, err := ValidatePackage(cb.Ctx()) require.NoError(t, err) assert.IsError(t, errors[0], "HLKQWDCMRN") assert.Equal(t, "MinLength: length of \"foo\" must not be less than 7", errors[0].Description) }
func TestValidate_error1(t *testing.T) { cb := tests.New().TempGopath(true) defer cb.Cleanup() path, dir := cb.TempPackage("b", map[string]string{ "b.json": `{ "description": "b", "type": "system:type", "id": "b", "fields": { "b": { "type": "system:@string", "min-length": 10, "max-length": 5 } } }`, }) cb.Path(path).Dir(dir).Jsystem().Sauto(parser.Parse) errors, err := ValidatePackage(cb.Ctx()) // @string is invalid because minLength > maxLength require.NoError(t, err) assert.IsError(t, errors[0], "KULDIJUYFB") }
func TestCircularImport(t *testing.T) { cb := tests.New().TempGopath(true) defer cb.Cleanup() pathA, dirA := cb.TempPackage("a", map[string]string{}) pathB, dirB := cb.TempPackage("b", map[string]string{}) ioutil.WriteFile(filepath.Join(dirA, "package.json"), []byte(` { "type": "system:package", "aliases": { "b": "`+pathB+`" } } `), 0777) ioutil.WriteFile(filepath.Join(dirB, "package.json"), []byte(` { "type": "system:package", "aliases": { "a": "`+pathA+`" } } `), 0777) cb.Path(pathA).Dir(dirA).Cmd().Sempty().Jsystem() _, err := Parse(cb.Ctx(), pathA) assert.IsError(t, err, "NOVMGYKHHI") assert.HasError(t, err, "SCSCFJPPHD") }
func TestComparePackageHash(t *testing.T) { cb := tests.New().TempGopath(true) path, _ := cb.TempPackage("a", map[string]string{ "a.yaml": "type: system:package", }) cb.Path(path).Jempty().Spkg(path) // "a.b/c" not found in scache. changes, err := comparePackageHash(cb.Ctx(), "a.b/c") assert.IsError(t, err, "NHXWLPHCHL") assert.False(t, changes) // path not found in jcache changes, err = comparePackageHash(cb.Ctx(), path) require.NoError(t, err) assert.True(t, changes) cb.Jsystem().Jpkg(path, 999).Sauto(parser.Parse) // hash changed changes, err = comparePackageHash(cb.Ctx(), path) require.NoError(t, err) assert.True(t, changes) scache := sysctx.FromContext(cb.Ctx()) pi, _ := scache.Get(path) cb.Jpkg(path, pi.Hash) // hash correct changes, err = comparePackageHash(cb.Ctx(), path) require.NoError(t, err) assert.False(t, changes) pi.Aliases["c"] = "a.b/c" changes, err = comparePackageHash(cb.Ctx(), path) assert.IsError(t, err, "DGJTLHQOCQ") assert.False(t, changes) pi1 := scache.Set("a.b/c") changes, err = comparePackageHash(cb.Ctx(), path) require.NoError(t, err) assert.True(t, changes) cb.Jpkg("a.b/c", 1) pi1.Hash = 2 changes, err = comparePackageHash(cb.Ctx(), path) require.NoError(t, err) assert.True(t, changes) pi1.Hash = 1 changes, err = comparePackageHash(cb.Ctx(), path) require.NoError(t, err) assert.False(t, changes) }
func TestValidateNode(t *testing.T) { cb := tests.New() errors, err := ValidateNode(cb.Ctx(), &node.Node{Value: nil}) require.NoError(t, err) assert.Equal(t, 0, len(errors)) errors, err = ValidateNode(cb.Ctx(), &node.Node{Value: 1, Null: true}) require.NoError(t, err) assert.Equal(t, 0, len(errors)) errors, err = ValidateNode(cb.Ctx(), &node.Node{Value: 1, Missing: true}) require.NoError(t, err) assert.Equal(t, 0, len(errors)) }
func TestInt(t *testing.T) { cb := tests.New() defer cb.Cleanup() _, err := runKe(cb, "a", map[string]string{ "gallery.yaml": ` type: system:type id: gallery fields: images: type: system:@map items: type: "@photo" rules: - selector: "{photo} .width" type: system:@int minimum: 800`, "rectangle.yaml": ` type: system:type id: rectangle fields: width: type: system:@int height: type: system:@int`, "photo.yaml": ` type: system:type id: photo fields: size: type: "@rectangle"`, "faces.yaml": ` type: gallery id: faces images: foo: type: photo size: type: rectangle width: 500 height: 500`, }) assert.Error(t, err) assert.Contains(t, err.Error(), "Minimum: value 500 must not be less than 800") }
func TestNoIdError(t *testing.T) { cb := tests.New().TempGopath(true) defer cb.Cleanup() pathA, dirA := cb.TempPackage("a", map[string]string{ "a.json": `{ "type": "system:type" }`, }) cb.Path(pathA).Dir(dirA).Cmd().Sempty().Jsystem() _, err := Parse(cb.Ctx(), pathA) assert.IsError(t, err, "VFUNPHUFHD") assert.HasError(t, err, "DLLMKTDYFW") }
func TestTypesUnknownType(t *testing.T) { cb := tests.New().TempGopath(true) defer cb.Cleanup() pathA, dirA := cb.TempPackage("a", map[string]string{ "a.json": `{ "type": "system:type", "id": "a", "rules": [ { "type": "system:foo" } ] }`, }) cb.Path(pathA).Dir(dirA).Cmd().Sempty().Jsystem() _, err := Parse(cb.Ctx(), pathA) // unknown types are tolerated when scanning types require.NoError(t, err) cb.TempFile("b.json", `{ "type": "system:package", "rules": [ { "type": "foo:bar" } ] }`) _, err = Parse(cb.Ctx(), pathA) // b.json is not a type so unknown packages and types are permitted require.NoError(t, err) cb.TempFile("c.json", `{ "type": "system:type", "id": "c", "rules": [ { "type": "foo:bar" } ] }`) _, err = Parse(cb.Ctx(), pathA) // unknown packages are not tolerated when scanning types assert.HasError(t, err, "DKKFLKDKYI") }
func TestParse(t *testing.T) { cb := tests.New().TempGopath(true) defer cb.Cleanup() files := map[string]string{ "a.json": `{ "type": "system:type", "id": "a", "rule": { "type": "system:type", "embed": ["system:rule"], "fields": { "a": { "type": "system:@bool" } } } }`, "b.json": `{ "type": "system:type", "id": "b", "fields": { "c": { "type": "@a", "a": true, "optional": true }, "d": { "type": "@b" } } }`, } path, dir := cb.TempPackage("a", files) path = "kego.io/system" cb.Path(path).Dir(dir).Cmd().Sempty().Jsystem() pi, err := Parse(cb.Ctx(), path) require.NoError(t, err) _, err = generate.Structs(cb.Ctx(), pi.Env) require.NoError(t, err) }
func TestGoGet(t *testing.T) { cb := tests.New().TempGopath(true).Cmd() defer cb.Cleanup() pathA, _ := cb.TempPackage("a", map[string]string{ "a.json": `{ "type": "system:type", "id": "a" }`, }) err := GoGet(cb.Ctx(), pathA) require.NoError(t, err) cb.CmdUpdate(true) err = GoGet(cb.Ctx(), pathA) assert.IsError(t, err, "NIKCKQAKUI") }
func TestRules(t *testing.T) { cb := tests.New() defer cb.Cleanup() _, err := runKe(cb, "a", map[string]string{ "gallery.yaml": ` type: system:type id: gallery fields: str: type: system:@string rules: - selector: ":root" type: system:@string equal: foo`, "faces.yaml": ` type: gallery id: faces str: foo`, }) require.NoError(t, err) _, err = runKe(cb, "b", map[string]string{ "gallery.yaml": ` type: system:type id: gallery fields: str: type: system:@string rules: - selector: ":root" type: system:@string equal: foo`, "faces.yaml": ` type: gallery id: faces str: bar`, }) assert.Error(t, err) assert.Contains(t, err.Error(), "Equal: value \"bar\" must equal 'foo'") }
func TestGenerate_path(t *testing.T) { cb := tests.New().TempGopath(true) defer cb.Cleanup() path, dir := cb.TempPackage("z", map[string]string{ "a.json": `{"type": "system:type", "id": "a", "fields": {"a": {"type": "system:@string"}}}`, }) cb.Path(path).Dir(dir).Cmd().Wg().Jsystem().Sauto(parser.Parse) err := Generate(cb.Ctx(), cb.Env()) require.NoError(t, err) genBytes, err := ioutil.ReadFile(filepath.Join(dir, "generated.go")) require.NoError(t, err) assert.Contains(t, string(genBytes), "package z\n") }
func TestNoTypeError(t *testing.T) { cb := tests.New().TempGopath(true) defer cb.Cleanup() pathA, dirA := cb.TempPackage("a", map[string]string{ "a.json": `{ "id": "a" }`, }) cb.Path(pathA).Dir(dirA).Cmd().Sempty().Jsystem() _, err := Parse(cb.Ctx(), pathA) assert.IsError(t, err, "GJRHNGGWFD") assert.HasError(t, err, "MSNIGTIDIO") err = scanForTypesAndExports(cb.Ctx(), cb.Env(), nil, &PackageHasher{}) assert.IsError(t, err, "NUKWIHYFMQ") }
func TestRegisterTypes(t *testing.T) { cb := tests.New().Sempty().Jauto() imports := map[string]shared.ImportInfo{ "a": { Path: "b", Types: map[string]shared.TypeInfo{ "c": {Bytes: []byte(`{"type": "system:type", "id": "d"}`)}, }, }, "g": { Path: "h", Types: map[string]shared.TypeInfo{ "i": {Bytes: []byte(`{"type": "system:type", "id": "j"}`)}, }, }, } pi, err := registerTypes(cb.Ctx(), "b", imports) require.NoError(t, err) ti, ok := pi.Types.Get("d") assert.True(t, ok) assert.Equal(t, "b:d", ti.Type.(*system.Type).Id.String()) sc := sysctx.FromContext(cb.Ctx()) pi, ok = sc.Get("b") assert.True(t, ok) ti, ok = pi.Types.Get("d") assert.True(t, ok) assert.Equal(t, "b:d", ti.Type.(*system.Type).Id.String()) pi, ok = sc.Get("h") assert.True(t, ok) ti, ok = pi.Types.Get("j") assert.True(t, ok) assert.Equal(t, "h:j", ti.Type.(*system.Type).Id.String()) }
func TestDefaultInterfaceNativeType(t *testing.T) { cb := tests.New() defer cb.Cleanup() _, err := runKe(cb, "a", map[string]string{ "foo.yaml": ` type: system:type id: foo fields: baz: type: system:@string interface: true`, "bar.yaml": ` type: foo id: bar baz: qux`, }) require.NoError(t, err) }
func TestNeedsDummyRule(t *testing.T) { cb := tests.New() defer cb.Cleanup() _, err := runKe(cb, "a", map[string]string{ "foo.yaml": ` type: system:type id: foo fields: bar: type: system:@string rule: type: system:type embed: ["system:rule"] fields: default: type: "@foo"`, }) require.NoError(t, err) }
func TestSelector(t *testing.T) { cb := tests.New() defer cb.Cleanup() _, err := runKe(cb, "a", map[string]string{ "gallery.yaml": ` type: system:type id: gallery fields: images: type: system:@map items: type: "@photo" rules: - selector: ".protocol" type: system:@string equal: https`, "photo.yaml": ` type: system:type id: photo fields: protocol: type: system:@string default: http optional: true`, "faces.yaml": ` type: gallery id: faces images: foo: type: photo protocol: http`, }) assert.Error(t, err) assert.Contains(t, err.Error(), "Equal: value \"http\" must equal 'https'") }
func TestInitialise(t *testing.T) { cb := tests.New().TempGopath(true) defer cb.Cleanup() pathA, dirA := cb.TempPackage("a", map[string]string{ "a.json": `{"type": "system:type", "id": "a"}`, "a.go": "package a", }) pathB, dirB := cb.TempPackage("b", map[string]string{ "b.json": `{"type": "system:type", "id": "b"}`, "b.go": "package b", }) cb.OsWd(dirA) ctx, _, err := Initialise(cb.Ctx(), nil) require.NoError(t, err) env := envctx.FromContext(ctx) assert.Equal(t, dirA, env.Dir) assert.Equal(t, pathA, env.Path) cb.OsWd("/") ctx, _, err = Initialise(ctx, &Options{ Path: pathB, }) env = envctx.FromContext(ctx) require.NoError(t, err) assert.Equal(t, dirB, env.Dir) assert.Equal(t, pathB, env.Path) _, _, err = Initialise(ctx, &Options{ Path: "", }) assert.IsError(t, err, "ADNJKTLAWY") assert.HasErrorExternal(t, err, "CXOETFPTGM") }
func New(t *testing.T) *ClientContextBuilder { cb := &ClientContextBuilder{Base: tests.New()} cb.t = t cb.mock = gomock.NewController(t) return cb }