|
| 1 | +#!/bin/bash |
| 2 | +# Copyright 2025 Google LLC |
| 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 | +# This script ensures that every .go file within the Go snippets directory |
| 17 | +# is referenced in the files_to_test.txt file. This prevents new snippets |
| 18 | +# from being added without being included in the regression test suite. |
| 19 | + |
| 20 | +# --- Configuration --- |
| 21 | +RED='\033[0;31m' |
| 22 | +NC='\033[0m' # No Color |
| 23 | +EXIT_CODE=0 |
| 24 | +SNIPPETS_FILE="tools/go-snippets/files_to_test.txt" |
| 25 | + |
| 26 | +# --- Logic --- |
| 27 | +echo "Checking for Go files that are not registered in ${SNIPPETS_FILE}..." |
| 28 | + |
| 29 | +# Find all .go files in the snippets directory, excluding _test.go files. |
| 30 | +all_go_files=$(find examples/go -type f -name "*.go" ! -name "*_test.go" | sed 's|examples/go/||' | sort) |
| 31 | + |
| 32 | +# Extract all .go file paths from the snippets file, ignoring comments and arguments. |
| 33 | +referenced_files=$(grep -v '^\s*#' "${SNIPPETS_FILE}" | grep -o '[a-zA-Z0-9/._-]*\.go' | sort | uniq) |
| 34 | + |
| 35 | +# Compare the list of all .go files with the list of referenced files. |
| 36 | +unreferenced_files=$(comm -23 <(echo "${all_go_files}") <(echo "${referenced_files}")) |
| 37 | + |
| 38 | +if [[ -n "${unreferenced_files}" ]]; then |
| 39 | + echo -e "${RED}Error: The following Go files were found but are not referenced in ${SNIPPETS_FILE}:${NC}" |
| 40 | + # Indent the list of files for readability. |
| 41 | + echo "${unreferenced_files}" | sed 's/^/ /' |
| 42 | + echo |
| 43 | + echo "Please add them to ${SNIPPETS_FILE} to include them in the regression tests." |
| 44 | + EXIT_CODE=1 |
| 45 | +else |
| 46 | + echo "All Go files are correctly referenced in the snippets file." |
| 47 | +fi |
| 48 | + |
| 49 | +exit ${EXIT_CODE} |
0 commit comments