github.com/jasonfriedland/openapi2proto@v0.2.1/proto_test.go (about)

     1  package openapi2proto_test
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	"net/http"
     7  	"os"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/NYTimes/openapi2proto"
    12  	"github.com/NYTimes/openapi2proto/compiler"
    13  	"github.com/pmezard/go-difflib/difflib"
    14  )
    15  
    16  type genProtoTestCase struct {
    17  	options     bool
    18  	fixturePath string
    19  	wantProto   string
    20  	remoteFiles []string
    21  }
    22  
    23  func testGenProto(t *testing.T, tests ...genProtoTestCase) {
    24  	t.Helper()
    25  	origin, _ := os.Getwd()
    26  	for _, test := range tests {
    27  		t.Run(test.fixturePath, func(t *testing.T) {
    28  			for _, remoteFile := range test.remoteFiles {
    29  				res, err := http.Get(remoteFile)
    30  				if err != nil || res.StatusCode != http.StatusOK {
    31  					t.Skip(`Remote file ` + remoteFile + ` is not available`)
    32  				}
    33  			}
    34  
    35  			var generated bytes.Buffer
    36  			var compilerOptions []compiler.Option
    37  			if test.options {
    38  				compilerOptions = append(compilerOptions, compiler.WithAnnotation(true))
    39  			}
    40  			if err := openapi2proto.Transpile(&generated, test.fixturePath, openapi2proto.WithCompilerOptions(compilerOptions...)); err != nil {
    41  				t.Errorf(`failed to transpile: %s`, err)
    42  				return
    43  			}
    44  
    45  			os.Chdir(origin)
    46  			// if test.wantProto is empty, guess file name from the original
    47  			// fixture path
    48  			wantProtoFile := test.wantProto
    49  			if wantProtoFile == "" {
    50  				i := strings.LastIndexByte(test.fixturePath, '.')
    51  				if i > -1 {
    52  					wantProtoFile = test.fixturePath[:i] + `.proto`
    53  				} else {
    54  					t.Fatalf(`unable to guess proto file name from %s`, test.fixturePath)
    55  				}
    56  			}
    57  			want, err := ioutil.ReadFile(wantProtoFile)
    58  			if err != nil {
    59  				t.Fatal("unable to open test fixture: ", err)
    60  			}
    61  
    62  			if string(want) != generated.String() {
    63  				diff := difflib.UnifiedDiff{
    64  					A:        difflib.SplitLines(string(want)),
    65  					B:        difflib.SplitLines(generated.String()),
    66  					FromFile: wantProtoFile,
    67  					ToFile:   "Generated",
    68  					Context:  3,
    69  				}
    70  				text, _ := difflib.GetUnifiedDiffString(diff)
    71  				t.Errorf("testYaml (%s) differences:\n%s",
    72  					test.fixturePath, text)
    73  			}
    74  		})
    75  	}
    76  }
    77  
    78  func TestNetwork(t *testing.T) {
    79  	testGenProto(t, genProtoTestCase{
    80  		fixturePath: "fixtures/petstore/swagger.yaml",
    81  		remoteFiles: []string{
    82  			"https://raw.githubusercontent.com/NYTimes/openapi2proto/master/fixtures/petstore/Pet.yaml",
    83  		},
    84  	})
    85  }
    86  
    87  func TestGenerateProto(t *testing.T) {
    88  	tests := []genProtoTestCase{
    89  		{
    90  			fixturePath: "fixtures/cats.yaml",
    91  		},
    92  		{
    93  			fixturePath: "fixtures/catsanddogs.yaml",
    94  		},
    95  		{
    96  			fixturePath: "fixtures/semantic_api.json",
    97  		},
    98  		{
    99  			fixturePath: "fixtures/semantic_api.yaml",
   100  		},
   101  		{
   102  			fixturePath: "fixtures/most_popular.json",
   103  		},
   104  		{
   105  			fixturePath: "fixtures/spec.yaml",
   106  		},
   107  		{
   108  			fixturePath: "fixtures/spec.json",
   109  		},
   110  		{
   111  			options:     true,
   112  			fixturePath: "fixtures/semantic_api.json",
   113  			wantProto:   "fixtures/semantic_api-options.proto",
   114  		},
   115  		{
   116  			options:     true,
   117  			fixturePath: "fixtures/most_popular.json",
   118  			wantProto:   "fixtures/most_popular-options.proto",
   119  		},
   120  		{
   121  			options:     true,
   122  			fixturePath: "fixtures/spec.yaml",
   123  			wantProto:   "fixtures/spec-options.proto",
   124  		},
   125  		{
   126  			options:     true,
   127  			fixturePath: "fixtures/spec.json",
   128  			wantProto:   "fixtures/spec-options.proto",
   129  		},
   130  
   131  		{
   132  			fixturePath: "fixtures/includes_query.json",
   133  		},
   134  		{
   135  			fixturePath: "fixtures/lowercase_def.json",
   136  		},
   137  		{
   138  			fixturePath: "fixtures/missing_type.json",
   139  		},
   140  		/*
   141  			{
   142  				fixturePath: "fixtures/kubernetes.json",
   143  			},
   144  		*/
   145  		{
   146  			fixturePath: "fixtures/accountv1-0.json",
   147  		},
   148  		{
   149  			fixturePath: "fixtures/refs.json",
   150  		},
   151  		{
   152  			fixturePath: "fixtures/refs.yaml",
   153  		},
   154  		{
   155  			fixturePath: "fixtures/integers.yaml",
   156  		},
   157  		{
   158  			fixturePath: "fixtures/global_options.yaml",
   159  		},
   160  		{
   161  			fixturePath: "fixtures/naming_conversion.yaml",
   162  		},
   163  		{
   164  			options:     true,
   165  			fixturePath: "fixtures/custom_options.yaml",
   166  		},
   167  	}
   168  	testGenProto(t, tests...)
   169  }