diff --git a/commands/check.rb b/commands/check.rb index 156445f09..a57dac046 100644 --- a/commands/check.rb +++ b/commands/check.rb @@ -64,6 +64,7 @@ class Command # Run property and buildsystem tests on the package, and fail if they fail. return false unless system "#{CREW_LIB_PATH}/tests/prop_test #{name}" return false unless system "#{CREW_LIB_PATH}/tests/buildsystem_test #{name}" + return false if (ARGV[0] == 'check') && !system("#{CREW_LIB_PATH}/tests/package_test #{name}") # If we're still here every test has passed, so return true. return true diff --git a/tests/package/README.md b/tests/package/README.md new file mode 100644 index 000000000..0d320bbd0 --- /dev/null +++ b/tests/package/README.md @@ -0,0 +1,27 @@ +## Package Tests + +These are simple bash shell scripts that test package binaries, manpages, etc. to make sure they are working correctly and do not generate errors. A test filename has the same name as the package without any extension, should be executable and have at least one line to validate the test(s). It should be placed in a subdirectory with the first letter of the package name. The subdirectory is necessary due to the sheer volume of packages and helps to organize files for manageability. This is similar to the directory structure of the manifest filelists. Keep in mind package tests will fail if the package is not installed. + +### Running Tests + +```bash +$ crew check [ ...] +``` +where <name> is the crew package. If the package test exists, it will run last in the check sequence. + +### Examples + +coreutils: +```bash +#!/bin/bash +for b in $(crew files coreutils | grep /usr/local/bin); do $b --version; done +``` +This test is in the `c` directory. + +github_cli: +```bash +#!/bin/bash +man gh | cat +gh --version +``` +This test is in the `g` directory. diff --git a/tests/package/c/coreutils b/tests/package/c/coreutils new file mode 100755 index 000000000..ca8b0fd23 --- /dev/null +++ b/tests/package/c/coreutils @@ -0,0 +1,2 @@ +#!/bin/bash +for b in $(crew files coreutils | grep /usr/local/bin); do $b --version; done diff --git a/tests/package/g/github_cli b/tests/package/g/github_cli new file mode 100755 index 000000000..100dc6606 --- /dev/null +++ b/tests/package/g/github_cli @@ -0,0 +1,3 @@ +#!/bin/bash +man gh | cat +gh --version diff --git a/tests/package_test b/tests/package_test new file mode 100755 index 000000000..ecba1a371 --- /dev/null +++ b/tests/package_test @@ -0,0 +1,58 @@ +#!/usr/bin/env ruby + +require_relative '../lib/const' +require_relative '../lib/color' +require_relative '../lib/package' +require_relative '../lib/package_utils' + +# Add >LOCAL< lib to LOAD_PATH so that packages can be loaded +$LOAD_PATH.unshift File.join(CREW_LIB_PATH, 'lib') + +def check_package(pkg, verbose: false) + puts "Checking #{pkg.name} package ...".yellow if verbose + unless PackageUtils.installed?(pkg.name) + puts "Package #{pkg.name} is not installed.".lightred + puts "Package tests for #{pkg.name} failed.".lightred + return 1 + end + if File.exist? "#{CREW_LIB_PATH}/tests/package/#{pkg.name[0]}/#{pkg.name}" + stdout = `#{CREW_LIB_PATH}/tests/package/#{pkg.name[0]}/#{pkg.name} 2> /tmp/errout` + errout = `cat /tmp/errout`.chomp + else + stdout = '' + errout = "Package tests for #{pkg.name} do not exist. :(".lightred + end + print stdout + # Check if the tests passed. + if errout.empty? + puts "Package tests for #{pkg.name} passed.".lightgreen if verbose + return 0 + else + puts errout + puts "Package tests for #{pkg.name} failed.".lightred + return 1 + end +end + +if ARGV[0] + ARGV.each do |name| + if File.file?(File.join(CREW_PACKAGES_PATH, "#{name}.rb")) + test_result = check_package(Package.load_package(File.join(CREW_PACKAGES_PATH, "#{name}.rb")), verbose: true) + exit(1) if test_result.positive? + else + puts "Package #{name} not found.".lightred + end + end +else + failed_packages = 0 + Dir["#{CREW_PACKAGES_PATH}/*.rb"].each do |filename| + failed_packages += check_package(Package.load_package(filename)) + end + + if failed_packages.positive? + puts "\n#{failed_packages} packages failed package tests.".lightred + exit(1) + else + puts "\nAll packages passed package tests.".lightgreen + end +end