kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/extractors/config/preprocessor/modifier/modifier_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 modifier
    18  
    19  import (
    20  	"io"
    21  	"io/ioutil"
    22  	"os"
    23  	"path/filepath"
    24  	"strings"
    25  	"testing"
    26  
    27  	"kythe.io/kythe/go/test/testutil"
    28  )
    29  
    30  const testDataDir = "testdata"
    31  
    32  func TestBuildGradleHasKythe(t *testing.T) {
    33  	testcases := []struct {
    34  		fileName     string
    35  		javacWrapper string
    36  		// Whether kythe javac wrapper should be present.
    37  		hasWrapper bool
    38  		// Whether we expect an error.
    39  		wantError bool
    40  	}{
    41  		{fileName: "modified-build.gradle", javacWrapper: "/tmp/javac-wrapper.sh", hasWrapper: true},
    42  		{fileName: "plain-build.gradle", javacWrapper: "/tmp/javac-wrapper.sh"},
    43  		{fileName: "other-build.gradle", javacWrapper: "/tmp/javac-wrapper.sh", wantError: true},
    44  		// Look at other-build.gradle but use the correct javac wrapper.
    45  		{fileName: "other-build.gradle", javacWrapper: "/different/javac-wrapper.sh", hasWrapper: true},
    46  	}
    47  
    48  	for _, tcase := range testcases {
    49  		// This should just ignore the file and do nothing.
    50  		hasWrapper, err := hasKytheWrapper(getPath(t, tcase.fileName), tcase.javacWrapper)
    51  		if err != nil && !tcase.wantError {
    52  			t.Fatalf("Failed to open test gradle file: %v", err)
    53  		} else if err == nil && tcase.wantError {
    54  			t.Errorf("Expected a failure for file %s, but didn't get one.", tcase.fileName)
    55  		} else if tcase.hasWrapper != hasWrapper {
    56  			var errstr string
    57  			if tcase.hasWrapper {
    58  				errstr = "should"
    59  			} else {
    60  				errstr = "should not"
    61  			}
    62  			t.Errorf("file %s %s already have kythe javac-wrapper", tcase.fileName, errstr)
    63  		}
    64  	}
    65  }
    66  
    67  func TestBuildGradle(t *testing.T) {
    68  	testcases := []struct {
    69  		inputFile          string
    70  		javacWrapper       string
    71  		expectedOutputFile string
    72  	}{
    73  		{"modified-build.gradle", "/tmp/javac-wrapper.sh", "modified-build.gradle"},
    74  		{"plain-build.gradle", "/tmp/javac-wrapper.sh", "modified-build.gradle"},
    75  		{"plain-build.gradle", "/different/javac-wrapper.sh", "other-build.gradle"},
    76  	}
    77  
    78  	for _, tcase := range testcases {
    79  		t.Run(tcase.inputFile, func(t *testing.T) {
    80  			// Copy the file into a temp file.
    81  			tf, err := ioutil.TempFile("", tcase.inputFile)
    82  			if err != nil {
    83  				t.Fatalf("creating temp file: %v", err)
    84  			}
    85  			tfName := tf.Name()
    86  			defer os.Remove(tfName)
    87  			infile, err := os.Open(getPath(t, tcase.inputFile))
    88  			if err != nil {
    89  				t.Fatalf("opening file %s: %v", tcase.inputFile, err)
    90  			}
    91  			_, err = io.Copy(tf, infile)
    92  			if err != nil {
    93  				t.Fatalf("copying %s: %v", tcase.inputFile, err)
    94  			}
    95  			if err := infile.Close(); err != nil {
    96  				t.Fatalf("closing %s: %v", tcase.inputFile, err)
    97  			}
    98  			if err := tf.Close(); err != nil {
    99  				t.Fatalf("closing temp file: %v", err)
   100  			}
   101  
   102  			// Do the copy if necessary.
   103  			if err := PreProcessBuildGradle(tfName, tcase.javacWrapper); err != nil {
   104  				t.Fatalf("modifying gradle file %s: %v", tcase.inputFile, err)
   105  			}
   106  
   107  			// Compare results.
   108  			eq, diff := testutil.TrimmedEqual(mustReadBytes(t, tfName), mustReadBytes(t, getPath(t, tcase.expectedOutputFile)))
   109  			if !eq {
   110  				t.Errorf("Expected input file %s to be %s, but got diff %s", tcase.inputFile, tcase.expectedOutputFile, diff)
   111  			}
   112  		})
   113  	}
   114  }
   115  
   116  func TestPomXML(t *testing.T) {
   117  	testcases := []struct {
   118  		inputFile          string
   119  		expectedOutputFile string
   120  
   121  		// Specify this if there is an error expected, and include some snippet
   122  		// of text from the expected error to make sure it's the right one.
   123  		// If there's no expected error, just leave it as empty string.
   124  		expectedErrorSnippet string
   125  	}{
   126  		{"modified-pom.xml", "modified-pom.xml", ""},
   127  		{"child-pom.xml", "child-pom.xml", ""},
   128  		{"plain-pom.xml", "modified-pom.xml", ""},
   129  		{"other-pom.xml", "modified-pom.xml", ""},
   130  		{"plugin-management-pom.xml", "plugin-management-pom.xml", ""},
   131  		{"plugin-management-missing-version-pom.xml", "", "missing maven-compiler-plugin version"},
   132  		{"plugin-management-wrong-plugin-version-pom.xml", "", "unsupported maven-compiler-plugin version: 2.9.0"},
   133  		{"modified-wrong-source-version-pom.xml", "", "unsupported source version: 1.6"},
   134  		{"modified-wrong-target-version-pom.xml", "", "unsupported target version: 1.6"},
   135  		{"modified-invalid-version-pom.xml", "", "unsupported target version: elephant"},
   136  	}
   137  
   138  	for _, tcase := range testcases {
   139  		t.Run(tcase.inputFile, func(t *testing.T) {
   140  			// Copy the file into a temp file.
   141  			tf, err := ioutil.TempFile("", tcase.inputFile)
   142  			if err != nil {
   143  				t.Fatalf("creating temp file: %v", err)
   144  			}
   145  			tfName := tf.Name()
   146  			defer os.Remove(tfName)
   147  			infile, err := os.Open(getPath(t, tcase.inputFile))
   148  			if err != nil {
   149  				t.Fatalf("opening file %s: %v", tcase.inputFile, err)
   150  			}
   151  			_, err = io.Copy(tf, infile)
   152  			if err != nil {
   153  				t.Fatalf("copying %s: %v", tcase.inputFile, err)
   154  			}
   155  			if err := infile.Close(); err != nil {
   156  				t.Fatalf("closing %s: %v", tcase.inputFile, err)
   157  			}
   158  			if err := tf.Close(); err != nil {
   159  				t.Fatalf("closing temp file: %v", err)
   160  			}
   161  
   162  			// Do the copy if necessary.
   163  			procErr := PreProcessPomXML(tfName)
   164  			if procErr != nil && tcase.expectedErrorSnippet == "" {
   165  				t.Fatalf("modifying pom.xml %s: %v", tcase.inputFile, procErr)
   166  			} else if tcase.expectedErrorSnippet != "" {
   167  				if procErr == nil {
   168  					t.Errorf("expected error with text '%s', but got no error", tcase.expectedErrorSnippet)
   169  				} else if !strings.Contains(procErr.Error(), tcase.expectedErrorSnippet) {
   170  					t.Errorf("expected error with text '%s', but got %q", tcase.expectedErrorSnippet, procErr)
   171  				}
   172  				// Expected error with correct text, passes.
   173  			} else {
   174  				// No expected error, compare results.
   175  				eq, diff := testutil.TrimmedEqual(mustReadBytes(t, tfName), mustReadBytes(t, getPath(t, tcase.expectedOutputFile)))
   176  				if !eq {
   177  					t.Errorf("Expected input file %s to be %s, but got diff %s", tcase.inputFile, tcase.expectedOutputFile, diff)
   178  					t.Errorf("input: %s", mustReadBytes(t, tfName))
   179  				}
   180  			}
   181  		})
   182  	}
   183  }
   184  
   185  // getPath returns a resolved filepath name for a file living in testdata/ directory.
   186  func getPath(t *testing.T, f string) string {
   187  	return testutil.TestFilePath(t, filepath.Join(testDataDir, f))
   188  }
   189  
   190  func mustReadBytes(t *testing.T, f string) []byte {
   191  	t.Helper()
   192  	b, err := ioutil.ReadFile(f)
   193  	if err != nil {
   194  		t.Fatalf("Failed to read %s: %v", f, err)
   195  	}
   196  	return b
   197  }