X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=binapigen%2Fgenerate_test.go;fp=cmd%2Fbinapi-generator%2Fgenerate_test.go;h=5a2a07aab53b0a7369852cebe78ebee77fa7b856;hb=refs%2Fchanges%2F89%2F27589%2F2;hp=4bec874a092be6c18da071a06d2d71c4a6e913f5;hpb=280b1c6c83b676ef4e592f4ecf60cb5b54b6a753;p=govpp.git diff --git a/cmd/binapi-generator/generate_test.go b/binapigen/generate_test.go similarity index 82% rename from cmd/binapi-generator/generate_test.go rename to binapigen/generate_test.go index 4bec874..5a2a07a 100644 --- a/cmd/binapi-generator/generate_test.go +++ b/binapigen/generate_test.go @@ -1,53 +1,67 @@ -// Copyright (c) 2017 Cisco and/or its affiliates. +// Copyright (c) 2020 Cisco and/or its affiliates. // -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at: +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at: // -// http://www.apache.org/licenses/LICENSE-2.0 +// http://www.apache.org/licenses/LICENSE-2.0 // -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. -package main +package binapigen import ( - "bufio" - "io/ioutil" "os" "testing" . "github.com/onsi/gomega" + + "git.fd.io/govpp.git/binapigen/vppapi" ) -func TestGetInputFiles(t *testing.T) { - RegisterTestingT(t) - result, err := getInputFiles("testdata", 1) - Expect(err).ShouldNot(HaveOccurred()) - Expect(result).To(HaveLen(3)) - for _, file := range result { - Expect(file).To(BeAnExistingFile()) +const testOutputDir = "test_output_directory" + +func GenerateFromFile(apiDir, outputDir string, opts Options) error { + // parse API files + apifiles, err := vppapi.ParseDir(apiDir) + if err != nil { + return err } -} -func TestGetInputFilesError(t *testing.T) { - RegisterTestingT(t) - result, err := getInputFiles("nonexisting_directory", 1) - Expect(err).Should(HaveOccurred()) - Expect(result).To(BeNil()) + g, err := New(opts, apifiles) + if err != nil { + return err + } + for _, file := range g.Files { + if !file.Generate { + continue + } + GenerateBinapiFile(g, file, outputDir) + if file.Service != nil { + GenerateRPC(g, file, outputDir) + } + } + + if err = g.Generate(); err != nil { + return err + } + + return nil } func TestGenerateFromFile(t *testing.T) { RegisterTestingT(t) - outDir := "test_output_directory" + // remove directory created during test - defer os.RemoveAll(outDir) - err := generateFromFile("testdata/acl.api.json", outDir, nil) + defer os.RemoveAll(testOutputDir) + + err := GenerateFromFile("testdata/acl.api.json", testOutputDir, Options{}) Expect(err).ShouldNot(HaveOccurred()) - fileInfo, err := os.Stat(outDir + "/acl/acl.ba.go") + fileInfo, err := os.Stat(testOutputDir + "/acl/acl.ba.go") Expect(err).ShouldNot(HaveOccurred()) Expect(fileInfo.IsDir()).To(BeFalse()) Expect(fileInfo.Name()).To(BeEquivalentTo("acl.ba.go")) @@ -55,35 +69,36 @@ func TestGenerateFromFile(t *testing.T) { func TestGenerateFromFileInputError(t *testing.T) { RegisterTestingT(t) - outDir := "test_output_directory" - err := generateFromFile("testdata/nonexisting.json", outDir, nil) + + err := GenerateFromFile("testdata/nonexisting.json", testOutputDir, Options{}) Expect(err).Should(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("invalid input file name")) } func TestGenerateFromFileReadJsonError(t *testing.T) { RegisterTestingT(t) - outDir := "test_output_directory" - err := generateFromFile("testdata/input-read-json-error.json", outDir, nil) + + err := GenerateFromFile("testdata/input-read-json-error.json", testOutputDir, Options{}) Expect(err).Should(HaveOccurred()) Expect(err.Error()).To(ContainSubstring("invalid input file name")) } func TestGenerateFromFileGeneratePackageError(t *testing.T) { RegisterTestingT(t) - outDir := "test_output_directory" + // generate package throws panic, recover after it defer func() { if recovery := recover(); recovery != nil { t.Logf("Recovered from panic: %v", recovery) } - os.RemoveAll(outDir) + os.RemoveAll(testOutputDir) }() - err := generateFromFile("testdata/input-generate-error.json", outDir, nil) + + err := GenerateFromFile("testdata/input-generate-error.json", testOutputDir, Options{}) Expect(err).Should(HaveOccurred()) } -func TestGetContext(t *testing.T) { +/*func TestGetContext(t *testing.T) { RegisterTestingT(t) outDir := "test_output_directory" result, err := newContext("testdata/af_packet.api.json", outDir) @@ -109,31 +124,12 @@ func TestGetContextInterfaceJson(t *testing.T) { Expect(result).ToNot(BeNil()) Expect(result.outputFile) Expect(result.outputFile).To(BeEquivalentTo(outDir + "/ip/ip.ba.go")) -} +}*/ -func TestReadJson(t *testing.T) { - RegisterTestingT(t) - inputData, err := ioutil.ReadFile("testdata/af_packet.api.json") - Expect(err).ShouldNot(HaveOccurred()) - result, err := parseInputJSON(inputData) - Expect(err).ShouldNot(HaveOccurred()) - Expect(result).ToNot(BeNil()) - Expect(result.Len()).To(BeEquivalentTo(5)) -} - -func TestReadJsonError(t *testing.T) { - RegisterTestingT(t) - inputData, err := ioutil.ReadFile("testdata/input-read-json-error.json") - Expect(err).ShouldNot(HaveOccurred()) - result, err := parseInputJSON(inputData) - Expect(err).Should(HaveOccurred()) - Expect(result).To(BeNil()) -} - -func TestGeneratePackage(t *testing.T) { +/*func TestGeneratePackage(t *testing.T) { RegisterTestingT(t) // prepare context - testCtx := new(context) + testCtx := new(GenFile) testCtx.packageName = "test-package-name" // prepare input/output output files @@ -141,7 +137,7 @@ func TestGeneratePackage(t *testing.T) { Expect(err).ShouldNot(HaveOccurred()) jsonRoot, err := parseInputJSON(inputData) Expect(err).ShouldNot(HaveOccurred()) - testCtx.packageData, err = parsePackage(testCtx, jsonRoot) + testCtx.file, err = parseModule(testCtx, jsonRoot) Expect(err).ShouldNot(HaveOccurred()) outDir := "test_output_directory" outFile, err := os.Create(outDir) @@ -158,7 +154,7 @@ func TestGeneratePackage(t *testing.T) { func TestGenerateMessageType(t *testing.T) { RegisterTestingT(t) // prepare context - testCtx := new(context) + testCtx := new(GenFile) testCtx.packageName = "test-package-name" // prepare input/output output files @@ -169,18 +165,18 @@ func TestGenerateMessageType(t *testing.T) { outDir := "test_output_directory" outFile, err := os.Create(outDir) Expect(err).ShouldNot(HaveOccurred()) - testCtx.packageData, err = parsePackage(testCtx, jsonRoot) + testCtx.file, err = parseModule(testCtx, jsonRoot) Expect(err).ShouldNot(HaveOccurred()) defer os.RemoveAll(outDir) // prepare writer writer := bufio.NewWriter(outFile) - for _, msg := range testCtx.packageData.Messages { + for _, msg := range testCtx.file.Messages { generateMessage(testCtx, writer, &msg) Expect(writer.Buffered()).ToNot(BeZero()) } -} +}*/ /*func TestGenerateMessageName(t *testing.T) { RegisterTestingT(t)