diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml new file mode 100644 index 00000000..3c461f58 --- /dev/null +++ b/.github/workflows/build.yaml @@ -0,0 +1,26 @@ +name: "Build and test" + +on: + push: + branches: [main] + pull_request: {} + +permissions: + contents: read + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 + - name: Install Go + uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0 + with: + go-version-file: go.mod + - name: Install addlicense + run: go install github.com/google/addlicense@v1.1.1 + - name: Run tests + run: make test + - name: Run integration tests + run: make integration diff --git a/boilerplate/boilerplate.Dockerfile.txt b/boilerplate/boilerplate.Dockerfile.txt deleted file mode 100644 index 16a6dad2..00000000 --- a/boilerplate/boilerplate.Dockerfile.txt +++ /dev/null @@ -1,14 +0,0 @@ -# Copyright YEAR Google, Inc. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - diff --git a/boilerplate/boilerplate.Makefile.txt b/boilerplate/boilerplate.Makefile.txt deleted file mode 100644 index 16a6dad2..00000000 --- a/boilerplate/boilerplate.Makefile.txt +++ /dev/null @@ -1,14 +0,0 @@ -# Copyright YEAR Google, Inc. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - diff --git a/boilerplate/boilerplate.go.txt b/boilerplate/boilerplate.go.txt deleted file mode 100644 index 9138113b..00000000 --- a/boilerplate/boilerplate.go.txt +++ /dev/null @@ -1,16 +0,0 @@ -/* -Copyright YEAR Google, Inc. All rights reserved. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - diff --git a/boilerplate/boilerplate.py b/boilerplate/boilerplate.py deleted file mode 100755 index bcb36939..00000000 --- a/boilerplate/boilerplate.py +++ /dev/null @@ -1,170 +0,0 @@ -#!/usr/bin/env python - -# Copyright 2015 Google, Inc. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -from __future__ import print_function - -import argparse -import glob -import json -import mmap -import os -import re -import sys - -parser = argparse.ArgumentParser() -parser.add_argument("filenames", help="list of files to check, all files if unspecified", nargs='*') - -rootdir = os.path.dirname(__file__) + "/../" -rootdir = os.path.abspath(rootdir) -parser.add_argument("--rootdir", default=rootdir, help="root directory to examine") - -default_boilerplate_dir = os.path.join(rootdir, "/boilerplate") -parser.add_argument("--boilerplate-dir", default=default_boilerplate_dir) -args = parser.parse_args() - - -def get_refs(): - refs = {} - - for path in glob.glob(os.path.join(args.boilerplate_dir, "boilerplate.*.txt")): - extension = os.path.basename(path).split(".")[1] - - ref_file = open(path, 'r') - ref = ref_file.read().splitlines() - ref_file.close() - refs[extension] = ref - - return refs - -def file_passes(filename, refs, regexs): - try: - f = open(filename, 'r') - except: - return False - - data = f.read() - f.close() - - basename = os.path.basename(filename) - extension = file_extension(filename) - if extension != "": - ref = refs[extension] - else: - ref = refs[basename] - - # remove build tags from the top of Go files - if extension == "go": - p = regexs["go_build_constraints"] - (data, found) = p.subn("", data, 1) - - # remove shebang from the top of shell files - if extension == "sh": - p = regexs["shebang"] - (data, found) = p.subn("", data, 1) - - data = data.splitlines() - - # if our test file is smaller than the reference it surely fails! - if len(ref) > len(data): - return False - - # trim our file to the same number of lines as the reference file - data = data[:len(ref)] - - p = regexs["year"] - for d in data: - if p.search(d): - return False - - # Replace all occurrences of the regex "2018|2017|2016|2015|2014" with "YEAR" - p = regexs["date"] - for i, d in enumerate(data): - (data[i], found) = p.subn('YEAR', d) - if found != 0: - break - - # if we don't match the reference at this point, fail - if ref != data: - return False - - return True - -def file_extension(filename): - return os.path.splitext(filename)[1].split(".")[-1].lower() - -skipped_dirs = ['Godeps', 'third_party', '.git', "vendor", "differs/testDirs/pipTests"] - -def normalize_files(files): - newfiles = [] - for pathname in files: - if any(x in pathname for x in skipped_dirs): - continue - newfiles.append(pathname) - for i, pathname in enumerate(newfiles): - if not os.path.isabs(pathname): - newfiles[i] = os.path.join(args.rootdir, pathname) - return newfiles - -def get_files(extensions): - files = [] - if len(args.filenames) > 0: - files = args.filenames - else: - for root, dirs, walkfiles in os.walk(args.rootdir): - # don't visit certain dirs. This is just a performance improvement - # as we would prune these later in normalize_files(). But doing it - # cuts down the amount of filesystem walking we do and cuts down - # the size of the file list - for d in skipped_dirs: - if d in dirs: - dirs.remove(d) - - for name in walkfiles: - pathname = os.path.join(root, name) - files.append(pathname) - - files = normalize_files(files) - outfiles = [] - for pathname in files: - basename = os.path.basename(pathname) - extension = file_extension(pathname) - if extension in extensions or basename in extensions: - outfiles.append(pathname) - return outfiles - -def get_regexs(): - regexs = {} - # Search for "YEAR" which exists in the boilerplate, but shouldn't in the real thing - regexs["year"] = re.compile( 'YEAR' ) - # dates can be 2014, 2015, 2016, 2017, 2018, 2019 or 2020 company holder names can be anything - regexs["date"] = re.compile( '(2014|2015|2016|2017|2018|2019|2020)' ) - # strip // +build \n\n build constraints - regexs["go_build_constraints"] = re.compile(r"^(// \+build.*\n)+\n", re.MULTILINE) - # strip #!.* from shell scripts - regexs["shebang"] = re.compile(r"^(#!.*\n)\n*", re.MULTILINE) - return regexs - -def main(): - regexs = get_regexs() - refs = get_refs() - filenames = get_files(refs.keys()) - - for filename in filenames: - if not file_passes(filename, refs, regexs): - print(filename, file=sys.stdout) - -if __name__ == "__main__": - sys.exit(main()) diff --git a/boilerplate/boilerplate.py.txt b/boilerplate/boilerplate.py.txt deleted file mode 100644 index 820dc590..00000000 --- a/boilerplate/boilerplate.py.txt +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env python - -# Copyright YEAR Google, Inc. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - diff --git a/boilerplate/boilerplate.sh.txt b/boilerplate/boilerplate.sh.txt deleted file mode 100644 index 16a6dad2..00000000 --- a/boilerplate/boilerplate.sh.txt +++ /dev/null @@ -1,14 +0,0 @@ -# Copyright YEAR Google, Inc. All rights reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - diff --git a/differs/apt_diff.go b/differs/apt_diff.go index b1c85168..8c94859e 100644 --- a/differs/apt_diff.go +++ b/differs/apt_diff.go @@ -28,7 +28,7 @@ import ( "github.com/sirupsen/logrus" ) -//APT package database location +// APT package database location const dpkgStatusFile string = "var/lib/dpkg/status" type AptAnalyzer struct { diff --git a/differs/emerge_diff.go b/differs/emerge_diff.go index a7a70410..4e8ae6cb 100644 --- a/differs/emerge_diff.go +++ b/differs/emerge_diff.go @@ -28,7 +28,7 @@ import ( "github.com/sirupsen/logrus" ) -//Emerge package database location +// Emerge package database location const emergePkgFile string = "/var/db/pkg" type EmergeAnalyzer struct{} diff --git a/differs/rpm_diff.go b/differs/rpm_diff.go index 4449a721..a0b04e9a 100644 --- a/differs/rpm_diff.go +++ b/differs/rpm_diff.go @@ -45,10 +45,10 @@ import ( "github.com/sirupsen/logrus" ) -//RPM macros file location +// RPM macros file location const rpmMacros string = "/usr/lib/rpm/macros" -//RPM command to extract packages from the rpm database +// RPM command to extract packages from the rpm database var rpmCmd = []string{ "rpm", "--nodigest", "--nosignature", "-qa", "--qf", "%{NAME}\t%{VERSION}-%{RELEASE}\t%{SIZE}\n", diff --git a/pkg/util/fs_utils.go b/pkg/util/fs_utils.go index a9c87eb5..654102d7 100644 --- a/pkg/util/fs_utils.go +++ b/pkg/util/fs_utils.go @@ -54,7 +54,7 @@ func GetSize(path string) int64 { return stat.Size() } -//GetFileContents returns the contents of a file at the specified path +// GetFileContents returns the contents of a file at the specified path func GetFileContents(path string) (*string, error) { if _, err := os.Lstat(path); os.IsNotExist(err) { return nil, err diff --git a/test.sh b/test.sh index 30765700..4acf80ce 100755 --- a/test.sh +++ b/test.sh @@ -26,25 +26,5 @@ if [[ $files ]]; then exit 1 fi -# Check for python on host, and use it if possible, otherwise fall back on python dockerized -if [[ -f $(which python 2>&1) ]]; then - PYTHON="python" -else - PYTHON="docker run --rm -it -v $(pwd):/container-diff -w /container-diff python python" -fi - - -# Ignore these paths in the following tests. -ignore="vendor\|out\|actions\|setup-tests" - -# Check boilerplate -echo "Checking boilerplate..." -BOILERPLATEDIR=./boilerplate -# Grep returns a non-zero exit code if we don't match anything, which is good in this case. -set +e -files=$(${PYTHON} ${BOILERPLATEDIR}/boilerplate.py --rootdir . --boilerplate-dir ${BOILERPLATEDIR} | grep -v $ignore) -set -e -if [[ ! -z ${files} ]]; then - echo "Boilerplate missing in: ${files}." - exit 1 -fi +echo "Checking go-addlicense..." +addlicense -check -l apache -ignore \{actions,differs,vendor,setup-tests,out\}/**/* -ignore cloudbuild\*yaml * diff --git a/tests/file_diff_expected.json b/tests/file_diff_expected.json index 5dd18243..855715f1 100644 --- a/tests/file_diff_expected.json +++ b/tests/file_diff_expected.json @@ -6731,12 +6731,12 @@ "Size2": 10125 }, { - "Name": "/var/lib/rpm/Conflictname", + "Name": "/var/lib/rpm/Obsoletename", "Size1": 8192, "Size2": 8192 }, { - "Name": "/var/lib/rpm/Name", + "Name": "/var/lib/rpm/Group", "Size1": 8192, "Size2": 8192 }, @@ -6746,22 +6746,22 @@ "Size2": 8192 }, { - "Name": "/var/lib/rpm/Installtid", + "Name": "/var/lib/rpm/Sha1header", "Size1": 8192, "Size2": 8192 }, { - "Name": "/var/lib/rpm/Group", + "Name": "/var/lib/rpm/Conflictname", "Size1": 8192, "Size2": 8192 }, { - "Name": "/var/lib/rpm/Obsoletename", + "Name": "/var/lib/rpm/Name", "Size1": 8192, "Size2": 8192 }, { - "Name": "/var/lib/rpm/Sha1header", + "Name": "/var/lib/rpm/Installtid", "Size1": 8192, "Size2": 8192 }, @@ -6791,12 +6791,12 @@ "Size2": 546 }, { - "Name": "/var/lib/yum/rpmdb-indexes/version", + "Name": "/var/cache/yum/x86_64/7/timedhosts", "Size1": 44, "Size2": 44 }, { - "Name": "/var/cache/yum/x86_64/7/timedhosts", + "Name": "/var/lib/yum/rpmdb-indexes/version", "Size1": 44, "Size2": 44 } diff --git a/tests/integration_test.go b/tests/integration_test.go index 75ba0507..093f7a5f 100644 --- a/tests/integration_test.go +++ b/tests/integration_test.go @@ -1,3 +1,4 @@ +//go:build integration // +build integration /*