Add library test (#14017)

This commit is contained in:
Ed Reel
2026-01-02 18:31:34 -06:00
committed by GitHub
parent 57affd74fe
commit 098e79294a
3 changed files with 121 additions and 32 deletions

View File

@@ -8,6 +8,9 @@ class Command
def self.check(name, force)
local_package = File.join(CREW_LOCAL_REPO_ROOT, 'packages', "#{name}.rb")
crew_package = File.join(CREW_PACKAGES_PATH, "#{name}.rb")
local_filelist = File.join(CREW_LOCAL_REPO_ROOT, 'manifest', ARCH, name[0].to_s, "#{name}.filelist")
crew_filelist_path = File.join(CREW_LIB_PATH, 'manifest', ARCH, name[0].to_s)
crew_filelist = File.join(crew_filelist_path, "#{name}.filelist")
local_package_test = File.join(CREW_LOCAL_REPO_ROOT, 'tests', 'package', name[0].to_s, name)
crew_package_test_path = File.join(CREW_LIB_PATH, 'tests', 'package', name[0].to_s)
crew_package_test = File.join(crew_package_test_path, name)
@@ -21,7 +24,7 @@ class Command
# Use rubocop to sanitize package file, and let errors get flagged.
if Kernel.system('rubocop --version', %i[out err] => File::NULL)
puts "Using rubocop to sanitize #{local_package}".orange
puts "Using rubocop to sanitize #{local_package}".yellow
# The .rubocop.yml file is found in the rubocop-chromebrew gem
require_gem('rubocop-chromebrew')
if File.file?(File.join(CREW_LOCAL_REPO_ROOT, '.rubocop.yml'))
@@ -35,54 +38,56 @@ class Command
puts 'To install Rubocop, run the following command: '.lightred + "crew #{'re' if PackageUtils.installed?('ruby_rubocop')}install ruby_rubocop".lightblue
end
to_copy = force
to_copy_package = force
to_copy_test = force
to_copy_filelist = force
# This pulls the operation from the calling function.
operation = caller_locations(1, 2)[0].to_s.split[3].split('_')[0].split('#')[1]
# Prompt to copy the local repo package to crew if the package is not found.
unless force || File.file?(crew_package)
puts "The crew package '#{name}' does not exist."
to_copy = true
if !force && File.file?(local_package) && !File.file?(crew_package)
puts "The crew package #{crew_package} does not exist.".yellow
to_copy_package = true
end
# Prompt to copy the local repo package test to crew if the package test is not found.
if File.file?(local_package_test) && !(force || File.file?(crew_package_test))
puts "The crew package test '#{name}' does not exist."
if !force && File.file?(local_package_test) && !File.file?(crew_package_test)
puts "The crew package test #{crew_package_test} does not exist.".yellow
to_copy_test = true
end
# Prompt to copy the local repo filelist to crew if the filelist is not found.
if !force && File.file?(local_filelist) && !File.file?(crew_filelist)
puts "The crew filelist #{crew_filelist} does not exist.".yellow
to_copy_filelist = true
end
# Compare local repo package to the crew repo package and prompt to copy if necessary to prepare for the operation.
unless force || (File.file?(crew_package) && FileUtils.identical?(local_package, crew_package))
puts "#{local_package} does not match the crew package."
to_copy = true
if !force && File.file?(local_package) && File.file?(crew_package) && !FileUtils.identical?(local_package, crew_package)
puts "#{local_package} does not match the crew package.".yellow
to_copy_package = true
end
# Compare local repo package test to the crew repo package test and prompt to copy if necessary to prepare for the operation.
if File.file?(local_package_test) && !(force || (File.file?(crew_package_test) && FileUtils.identical?(local_package_test, crew_package_test)))
puts "#{local_package_test} does not match the crew package test."
if !force && File.file?(local_package_test) && File.file?(crew_package_test) && !FileUtils.identical?(local_package_test, crew_package_test)
puts "#{local_package_test} does not match the crew package test.".yellow
to_copy_test = true
end
if to_copy && !force
# This pulls the operation from the calling function.
operation = caller_locations(1, 2)[0].to_s.split[3].split('_')[0].split('#')[1]
if Package.agree_default_yes("\nWould you like to copy #{name}.rb to crew and start the #{operation}")
to_copy = true
else
return false
end
# Compare local repo filelist to the crew filelist and prompt to copy if necessary to prepare for the operation.
if !force && File.file?(local_filelist) && File.file?(crew_filelist) && !FileUtils.identical?(local_filelist, crew_filelist)
puts "#{local_filelist} does not match the crew filelist.".yellow
to_copy_filelist = true
end
if to_copy_test && !force && File.file?(local_package_test)
# This pulls the operation from the calling function.
operation = caller_locations(1, 2)[0].to_s.split[3].split('_')[0].split('#')[1]
if Package.agree_default_yes("\nWould you like to copy #{local_package_test} to crew and start the #{operation}")
to_copy_test = true
else
return false
end
end
return false if !force && to_copy_package && !Package.agree_default_yes("\nWould you like to copy #{local_package} to crew and start the #{operation}")
if to_copy
return false if !force && to_copy_test && !Package.agree_default_yes("\nWould you like to copy #{local_package_test} to crew and start the #{operation}")
return false if !force && to_copy_filelist && !Package.agree_default_yes("\nWould you like to copy #{local_filelist} to crew and start the #{operation}")
if to_copy_package && File.file?(local_package)
FileUtils.copy_file(local_package, crew_package)
puts "Copied #{local_package} to #{CREW_PACKAGES_PATH}".lightgreen
end
@@ -93,10 +98,19 @@ class Command
puts "Copied #{local_package_test} to #{crew_package_test_path}".lightgreen
end
if to_copy_filelist && File.file?(local_filelist)
FileUtils.mkdir_p crew_filelist_path unless File.directory?(crew_filelist_path)
FileUtils.copy_file(local_filelist, crew_filelist)
puts "Copied #{local_filelist} to #{crew_filelist_path}".lightgreen
end
# 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') && File.file?(local_package_test) && !system("#{CREW_LIB_PATH}/tests/package_test #{name}")
if ARGV[0] == 'check'
return false unless system("#{CREW_LIB_PATH}/tests/library_test #{name}")
return false unless system("#{CREW_LIB_PATH}/tests/package_test #{name}")
end
# If we're still here every test has passed, so return true.
return true

View File

@@ -4,7 +4,7 @@ require 'etc'
require 'open3'
OLD_CREW_VERSION = defined?(CREW_VERSION) ? CREW_VERSION : '1.0'
CREW_VERSION = '1.69.0' unless defined?(CREW_VERSION) && CREW_VERSION == OLD_CREW_VERSION
CREW_VERSION = '1.70.0' unless defined?(CREW_VERSION) && CREW_VERSION == OLD_CREW_VERSION
# Kernel architecture.
KERN_ARCH = Etc.uname[:machine]

75
tests/library_test Executable file
View File

@@ -0,0 +1,75 @@
#!/usr/bin/env ruby
require_relative '../lib/const'
require_relative '../lib/color'
require_relative '../lib/package'
# Add >LOCAL< lib to LOAD_PATH so that packages can be loaded
$LOAD_PATH.unshift File.join(CREW_LIB_PATH, 'lib')
def check_libraries(pkg_path, verbose: false)
name = File.basename(pkg_path, '.rb')
puts "Checking #{name} package ...".yellow if verbose
# If the package is invalid, it will almost certainly raise a NameError when we attempt to load it.
begin
Package.load_package(pkg_path)
rescue NameError => e
e.name.to_s
end
filelist = "#{CREW_LIB_PATH}/manifest/#{ARCH}/#{name[0]}/#{name}.filelist"
unless File.exist?(filelist)
puts "The manifest #{filelist} does not exist.".lightred
puts "Library test for #{name} failed.".lightred
return 1
end
libraries = `grep -E "^#{CREW_PREFIX}/lib" "#{filelist}" | grep -vE "^#{CREW_PREFIX}/libexec/" | grep -vE "^#{CREW_PREFIX}/lib/perl" | grep -vE "^#{CREW_PREFIX}/lib/python"`.chomp
if libraries.empty?
puts "Library test for #{name} passed.".lightgreen if verbose
return 0
else
valid_libraries = `grep -E "^#{CREW_LIB_PREFIX}/" "#{filelist}" | grep -vE "^#{CREW_PREFIX}/libexec/" | grep -vE "^#{CREW_PREFIX}/lib/perl" | grep -vE "^#{CREW_PREFIX}/lib/python"`.chomp
if valid_libraries.empty?
puts 'All libraries:'
puts libraries
puts 'Invalid library paths.'.lightred
puts "Library test for #{name} failed.".lightred
return 1
elsif libraries != valid_libraries
puts 'All libraries:'
puts libraries
puts 'Valid libraries:'
puts valid_libraries
puts 'Invalid library paths.'.lightred
puts "Library test for #{name} failed.".lightred
return 1
else
puts "Library test for #{name} passed.".lightgreen if verbose
return 0
end
end
end
if ARGV[0]
ARGV.each do |name|
if File.file?(File.join(CREW_PACKAGES_PATH, "#{name}.rb"))
test_result = check_libraries(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_libraries(filename)
end
if failed_packages.positive?
puts "\n#{failed_packages} packages failed library tests.".lightred
exit(1)
else
puts "\nAll packages passed library tests.".lightgreen
end
end