kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/extractors/config/runextractor/compdb/compdb_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 compdb
    18  
    19  import (
    20  	"context"
    21  	"os"
    22  	"path/filepath"
    23  	"reflect"
    24  	"testing"
    25  
    26  	"kythe.io/kythe/go/platform/kzip"
    27  )
    28  
    29  const (
    30  	testPath      = "kythe/go/extractors/config/runextractor/compdb"
    31  	extractorPath = "kythe/cxx/extractor/cxx_extractor"
    32  	workspace     = "io_kythe"
    33  )
    34  
    35  // setupEnvironment establishes the necessary environment variables and
    36  // current working directory for running tests.
    37  // Returns expected working directory.
    38  func setupEnvironment(t *testing.T) string {
    39  	t.Helper()
    40  	// TODO(shahms): ExtractCompilations should take an output path.
    41  	if _, ok := os.LookupEnv("TEST_TMPDIR"); !ok {
    42  		t.Skip("Skipping test due to incompatible environment (missing TEST_TMPDIR)")
    43  	}
    44  	output := t.TempDir()
    45  	if err := os.Setenv("KYTHE_OUTPUT_DIRECTORY", output); err != nil {
    46  		t.Fatalf("Error setting KYTHE_OUTPUT_DIRECTORY: %v", err)
    47  	}
    48  	root, err := os.Getwd()
    49  	if err != nil {
    50  		t.Fatalf("Unable to get working directory: %v", err)
    51  	}
    52  	return root
    53  }
    54  
    55  func testExtractCompilationsEndToEndWithDatabase(t *testing.T, compdbPath string) {
    56  	root := setupEnvironment(t)
    57  	defer os.Chdir(root)
    58  
    59  	extractor, err := filepath.Abs(extractorPath)
    60  	if err != nil {
    61  		t.Fatalf("Unable to get absolute path to extractor: %v", err)
    62  	}
    63  	// Paths in compilation_database.json are relative to the testdata directory, so change there.
    64  	if err := os.Chdir(filepath.Join(root, testPath, "testdata")); err != nil {
    65  		t.Fatalf("Unable to change working directory: %v", err)
    66  	}
    67  	if err := ExtractCompilations(context.Background(), extractor, compdbPath, nil); err != nil {
    68  		t.Fatalf("Error running ExtractCompilations: %v", err)
    69  	}
    70  	err = filepath.Walk(os.Getenv("KYTHE_OUTPUT_DIRECTORY"), func(path string, info os.FileInfo, err error) error {
    71  		if err != nil {
    72  			return err
    73  		} else if info.IsDir() {
    74  			return nil
    75  		} else if filepath.Ext(path) != ".kzip" {
    76  			t.Logf("Ignoring non-kzip file: %v", path)
    77  			return nil
    78  		}
    79  		reader, err := os.Open(path)
    80  		if err != nil {
    81  			return err
    82  		}
    83  		defer reader.Close()
    84  		err = kzip.Scan(reader, func(r *kzip.Reader, unit *kzip.Unit) error {
    85  			if !reflect.DeepEqual(unit.Proto.SourceFile, []string{"test_file.cc"}) {
    86  				t.Fatalf("Invalid source_file: %v", unit.Proto.SourceFile)
    87  			}
    88  			return nil
    89  		})
    90  		if err != nil {
    91  			return err
    92  		}
    93  		return nil
    94  	})
    95  	if err != nil {
    96  		t.Fatalf("Error processing extracted output: %v", err)
    97  	}
    98  
    99  }
   100  
   101  func TestExtractCompilationsEndToEnd(t *testing.T) {
   102  	t.Run("command", func(t *testing.T) {
   103  		testExtractCompilationsEndToEndWithDatabase(t, "compilation_database.json")
   104  	})
   105  	t.Run("arguments", func(t *testing.T) {
   106  		testExtractCompilationsEndToEndWithDatabase(t, "compilation_database_arguments.json")
   107  	})
   108  }