-// 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"))
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)
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
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)
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
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)