Add package tests (#13853)

This commit is contained in:
Ed Reel
2025-12-15 14:07:14 -06:00
committed by GitHub
parent 8bd112b869
commit ebedd7d7d1
5 changed files with 91 additions and 0 deletions

View File

@@ -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

27
tests/package/README.md Normal file
View File

@@ -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 <name> [<name2> <name3> ...]
```
where &lt;name&gt; 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.

2
tests/package/c/coreutils Executable file
View File

@@ -0,0 +1,2 @@
#!/bin/bash
for b in $(crew files coreutils | grep /usr/local/bin); do $b --version; done

3
tests/package/g/github_cli Executable file
View File

@@ -0,0 +1,3 @@
#!/bin/bash
man gh | cat
gh --version

58
tests/package_test Executable file
View File

@@ -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