mirror of
https://github.com/chromebrew/chromebrew.git
synced 2026-01-08 23:18:10 -05:00
Update tests (#5408)
* Update tests * Improve speed of cycle_test and load_test scripts
This commit is contained in:
@@ -2,22 +2,23 @@
|
||||
|
||||
## Why to run tests
|
||||
|
||||
Running these tests will make sure that crew is working correctly, or explain the problem if it's not.
|
||||
Running tests will make sure that crew is working correctly or explain the problem if it's not.
|
||||
|
||||
## When to run tests
|
||||
|
||||
The tests can be used at any time to help with developing, but make sure to run them before submitting a Pull Request.
|
||||
The tests can be used at any time to help with development but make sure to run them before submitting a Pull Request.
|
||||
|
||||
## Preparing new tests
|
||||
|
||||
To prepare a new test, create a `.rb` file. It may use crew constants (created in `lib/const.rb`) and import packages (be sure to use `require_relative`).
|
||||
To prepare a new test, create a ruby script. It may use crew constants (created in `lib/const.rb`) and import packages (be sure to use `require_relative`).
|
||||
|
||||
It should finish normally if the test passed, or finish with `exit 1` otherwise, preferably explaining why the test failed.
|
||||
All `.rb` files in this directory will automatically be run by the `all_tests` script.
|
||||
It should finish normally if the test passed, otherwise abort with `exit 1` and preferably explain why the test failed.
|
||||
|
||||
Here is a simple example of a test:
|
||||
A simple example of a test script called `my_test` is below:
|
||||
|
||||
```ruby
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
# Makes sure buildessential package depends on gcc
|
||||
|
||||
require_relative("../packages/buildessential")
|
||||
@@ -29,9 +30,10 @@ else
|
||||
exit 1
|
||||
end
|
||||
```
|
||||
Make sure the script is executable with `chmod +x my_test`. Note: This will only work in directories with execute permission.
|
||||
|
||||
## How to run tests
|
||||
|
||||
Execute `ruby test_all` from this directory (`tests/`). If all tests pass, it will print `All tests successful.`, otherwise it will show what went wrong.
|
||||
Execute `ruby my_test` from this directory (`tests/`). If all tests pass, it should display a message similar to `All tests successful.`, otherwise it should indicate what went wrong.
|
||||
|
||||
Running tests manually may not work!
|
||||
|
||||
38
tests/no_cycles.rb → tests/cycle_test
Normal file → Executable file
38
tests/no_cycles.rb → tests/cycle_test
Normal file → Executable file
@@ -1,19 +1,24 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
# This test checks whether the packages create a dependency cycle.
|
||||
|
||||
require 'find'
|
||||
require_relative '../lib/const'
|
||||
require_relative '../lib/color'
|
||||
|
||||
@all_pkgs = {}
|
||||
|
||||
$LOAD_PATH.unshift '../lib'
|
||||
|
||||
puts "Running dependency cycle tests...\n".yellow
|
||||
|
||||
# Loads all packages
|
||||
Find.find("#{CUR_DIR}/../packages") do |filename|
|
||||
if File.extname(filename) == '.rb'
|
||||
name = File.basename(filename, '.rb')
|
||||
require_relative("../packages/#{name}")
|
||||
pkg = Object.const_get(name.capitalize)
|
||||
pkg.name = name
|
||||
@all_pkgs[name] = pkg
|
||||
end
|
||||
Dir.glob('../packages/*.rb').each do |filename|
|
||||
name = File.basename(filename, '.rb')
|
||||
require_relative("../packages/#{name}")
|
||||
pkg = Object.const_get(name.capitalize)
|
||||
pkg.name = name
|
||||
@all_pkgs[name] = pkg
|
||||
end
|
||||
|
||||
# Looking for cycles. @path will keep the current dependency path.
|
||||
@@ -23,15 +28,17 @@ end
|
||||
@failed = 0
|
||||
@state = {}
|
||||
@path = []
|
||||
@uniq_path = []
|
||||
def dfs(pkg)
|
||||
@path.push(pkg.name)
|
||||
if @state[pkg] == :on_path
|
||||
puts "\nFound dependency cycle!".lightred
|
||||
while @path.first != @path.last
|
||||
@path.shift
|
||||
end
|
||||
puts @path.to_s
|
||||
@failed += 1
|
||||
if not @uniq_path.include? @path.to_s and @path.to_s.include? ','
|
||||
@uniq_path.push(@path.to_s)
|
||||
@failed += 1
|
||||
end
|
||||
elsif @state[pkg] == nil
|
||||
@state[pkg] = :on_path
|
||||
if pkg.dependencies
|
||||
@@ -51,11 +58,16 @@ end
|
||||
dfs(pkg)
|
||||
end
|
||||
|
||||
# Display dependency cycles
|
||||
@uniq_path.sort.each do |path|
|
||||
puts path.lightred
|
||||
end
|
||||
|
||||
@cycles = "cycles"
|
||||
@cycles = "cycle" if @failed == 1
|
||||
|
||||
if @failed > 0
|
||||
abort "\n#{@failed} dependency #{@cycles} found.".lightred
|
||||
abort "\n#{@failed} dependency #{@cycles} found.".lightred
|
||||
else
|
||||
puts "\nNo dependency cycles found.".lightgreen
|
||||
puts "\nNo dependency cycles found.".lightgreen
|
||||
end
|
||||
15
tests/load_test
Executable file
15
tests/load_test
Executable file
@@ -0,0 +1,15 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
require 'find'
|
||||
require_relative '../lib/const'
|
||||
require_relative '../lib/color'
|
||||
|
||||
# Add >LOCAL< lib to LOAD_PATH
|
||||
$LOAD_PATH.unshift '../lib'
|
||||
|
||||
Dir.glob('../packages/*.rb').each do |filename|
|
||||
puts "Loading #{File.basename(filename, '.rb')}..."
|
||||
load filename
|
||||
end
|
||||
|
||||
puts "\nAll load tests successful.".lightgreen
|
||||
@@ -1,21 +0,0 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
require 'find'
|
||||
require_relative '../lib/const'
|
||||
require_relative '../lib/color'
|
||||
|
||||
CUR_DIR = File.dirname(__FILE__)
|
||||
|
||||
# Add >LOCAL< lib to LOAD_PATH
|
||||
$LOAD_PATH.unshift "#{CUR_DIR}/../lib"
|
||||
|
||||
puts "Running tests..."
|
||||
|
||||
Find.find(CUR_DIR) do |filename|
|
||||
if File.extname(filename) == '.rb'
|
||||
puts "\nTest Name: #{File.basename(filename, ".rb")}"
|
||||
load filename
|
||||
end
|
||||
end
|
||||
|
||||
puts "\nAll tests successful.".lightgreen
|
||||
Reference in New Issue
Block a user