kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/extractors/gcp/config/converter_test.go (about)

     1  /*
     2   * Copyright 2018 The Kythe Authors. All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *   http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package config
    18  
    19  import (
    20  	"io/ioutil"
    21  	"path/filepath"
    22  	"testing"
    23  
    24  	"kythe.io/kythe/go/test/testutil"
    25  )
    26  
    27  const testDataDir = "testdata"
    28  
    29  // Also test some yaml configs checked in elsewhere.
    30  const examplesDir = "../../../../extractors/gcp/examples"
    31  
    32  func TestYAMLConversion(t *testing.T) {
    33  	testcases := []testcase{
    34  		{
    35  			name:     "maven-simple",
    36  			jsonFile: "mvn.json",
    37  			yamlDir:  testDataDir,
    38  			yamlFile: "mvn.yaml",
    39  		},
    40  		{
    41  			name:     "maven-subdir",
    42  			jsonFile: "mvn-subdir.json",
    43  			yamlDir:  testDataDir,
    44  			yamlFile: "mvn-subdir.yaml",
    45  		},
    46  		{
    47  			name:     "maven-multi",
    48  			jsonFile: "mvn-multi.json",
    49  			yamlDir:  testDataDir,
    50  			yamlFile: "mvn-multi.yaml",
    51  		},
    52  		{
    53  			name:     "maven-from-examples",
    54  			jsonFile: "mvn.json",
    55  			yamlDir:  examplesDir,
    56  			yamlFile: "mvn.yaml",
    57  		},
    58  		{
    59  			name:     "guava",
    60  			jsonFile: "guava-mvn.json",
    61  			yamlDir:  examplesDir,
    62  			yamlFile: "guava-mvn.yaml",
    63  		},
    64  		{
    65  			name:     "guava-android",
    66  			jsonFile: "guava-android-mvn.json",
    67  			yamlDir:  examplesDir,
    68  			yamlFile: "guava-android-mvn.yaml",
    69  		},
    70  		{
    71  			name:     "gradle-simple",
    72  			jsonFile: "gradle.json",
    73  			yamlDir:  testDataDir,
    74  			yamlFile: "gradle.yaml",
    75  		},
    76  		{
    77  			name:     "gradle-subdir",
    78  			jsonFile: "gradle-subdir.json",
    79  			yamlDir:  testDataDir,
    80  			yamlFile: "gradle-subdir.yaml",
    81  		},
    82  		{
    83  			name:     "gradle-from-examples",
    84  			jsonFile: "gradle.json",
    85  			yamlDir:  examplesDir,
    86  			yamlFile: "gradle.yaml",
    87  		},
    88  		{
    89  			name:     "bazel-from-examples",
    90  			jsonFile: "bazel.json",
    91  			yamlDir:  examplesDir,
    92  			yamlFile: "bazel.yaml",
    93  		},
    94  		{
    95  			name:     "bazel-multi",
    96  			jsonFile: "bazel-multi.json",
    97  			yamlDir:  testDataDir,
    98  			yamlFile: "bazel-multi.yaml",
    99  		},
   100  	}
   101  
   102  	for _, tcase := range testcases {
   103  		t.Run(tcase.name, func(t *testing.T) {
   104  			actual, err := KytheToYAML(tcase.getJSONFile(t))
   105  			if err != nil {
   106  				t.Fatalf("reading config %s: %v", tcase.jsonFile, err)
   107  			}
   108  			expected, err := tcase.getExpectedYAML(t)
   109  			if err != nil {
   110  				t.Fatalf("reading expected testdata %s/%s: %v", tcase.yamlDir, tcase.yamlFile, err)
   111  			}
   112  
   113  			if err := testutil.YAMLEqual(expected, actual); err != nil {
   114  				t.Errorf("Expected config %s to be equal: %v", tcase.jsonFile, err)
   115  			}
   116  		})
   117  	}
   118  }
   119  
   120  type testcase struct {
   121  	name     string
   122  	jsonFile string
   123  
   124  	// YAML files not guaranteed to be in testdata, could be elsewhere.
   125  	yamlDir  string
   126  	yamlFile string
   127  }
   128  
   129  func (tc testcase) getJSONFile(t *testing.T) string {
   130  	return testutil.TestFilePath(t, filepath.Join(testDataDir, tc.jsonFile))
   131  }
   132  
   133  func (tc testcase) getExpectedYAML(t *testing.T) ([]byte, error) {
   134  	fp := testutil.TestFilePath(t, filepath.Join(tc.yamlDir, tc.yamlFile))
   135  	return ioutil.ReadFile(fp)
   136  }