Commit 6403e4f0 authored by Ed Reel's avatar Ed Reel Committed by GitHub

Update tests (#5408)

* Update tests

* Improve speed of cycle_test and load_test scripts
parent e2863b90
...@@ -2,22 +2,23 @@ ...@@ -2,22 +2,23 @@
## Why to run tests ## 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 ## 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 ## 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. It should finish normally if the test passed, otherwise abort with `exit 1` and preferably explain why the test failed.
All `.rb` files in this directory will automatically be run by the `all_tests` script.
Here is a simple example of a test: A simple example of a test script called `my_test` is below:
```ruby ```ruby
#!/usr/bin/env ruby
# Makes sure buildessential package depends on gcc # Makes sure buildessential package depends on gcc
require_relative("../packages/buildessential") require_relative("../packages/buildessential")
...@@ -29,9 +30,10 @@ else ...@@ -29,9 +30,10 @@ else
exit 1 exit 1
end 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 ## 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! Running tests manually may not work!
#!/usr/bin/env ruby
# This test checks whether the packages create a dependency cycle. # This test checks whether the packages create a dependency cycle.
require 'find' require 'find'
require_relative '../lib/const'
require_relative '../lib/color' require_relative '../lib/color'
@all_pkgs = {} @all_pkgs = {}
$LOAD_PATH.unshift '../lib'
puts "Running dependency cycle tests...\n".yellow
# Loads all packages # Loads all packages
Find.find("#{CUR_DIR}/../packages") do |filename| Dir.glob('../packages/*.rb').each do |filename|
if File.extname(filename) == '.rb' name = File.basename(filename, '.rb')
name = File.basename(filename, '.rb') require_relative("../packages/#{name}")
require_relative("../packages/#{name}") pkg = Object.const_get(name.capitalize)
pkg = Object.const_get(name.capitalize) pkg.name = name
pkg.name = name @all_pkgs[name] = pkg
@all_pkgs[name] = pkg
end
end end
# Looking for cycles. @path will keep the current dependency path. # Looking for cycles. @path will keep the current dependency path.
...@@ -23,15 +28,17 @@ end ...@@ -23,15 +28,17 @@ end
@failed = 0 @failed = 0
@state = {} @state = {}
@path = [] @path = []
@uniq_path = []
def dfs(pkg) def dfs(pkg)
@path.push(pkg.name) @path.push(pkg.name)
if @state[pkg] == :on_path if @state[pkg] == :on_path
puts "\nFound dependency cycle!".lightred
while @path.first != @path.last while @path.first != @path.last
@path.shift @path.shift
end end
puts @path.to_s if not @uniq_path.include? @path.to_s and @path.to_s.include? ','
@failed += 1 @uniq_path.push(@path.to_s)
@failed += 1
end
elsif @state[pkg] == nil elsif @state[pkg] == nil
@state[pkg] = :on_path @state[pkg] = :on_path
if pkg.dependencies if pkg.dependencies
...@@ -51,11 +58,16 @@ end ...@@ -51,11 +58,16 @@ end
dfs(pkg) dfs(pkg)
end end
# Display dependency cycles
@uniq_path.sort.each do |path|
puts path.lightred
end
@cycles = "cycles" @cycles = "cycles"
@cycles = "cycle" if @failed == 1 @cycles = "cycle" if @failed == 1
if @failed > 0 if @failed > 0
abort "\n#{@failed} dependency #{@cycles} found.".lightred abort "\n#{@failed} dependency #{@cycles} found.".lightred
else else
puts "\nNo dependency cycles found.".lightgreen puts "\nNo dependency cycles found.".lightgreen
end end
...@@ -4,18 +4,12 @@ require 'find' ...@@ -4,18 +4,12 @@ require 'find'
require_relative '../lib/const' require_relative '../lib/const'
require_relative '../lib/color' require_relative '../lib/color'
CUR_DIR = File.dirname(__FILE__)
# Add >LOCAL< lib to LOAD_PATH # Add >LOCAL< lib to LOAD_PATH
$LOAD_PATH.unshift "#{CUR_DIR}/../lib" $LOAD_PATH.unshift '../lib'
puts "Running tests..."
Find.find(CUR_DIR) do |filename| Dir.glob('../packages/*.rb').each do |filename|
if File.extname(filename) == '.rb' puts "Loading #{File.basename(filename, '.rb')}..."
puts "\nTest Name: #{File.basename(filename, ".rb")}" load filename
load filename
end
end end
puts "\nAll tests successful.".lightgreen puts "\nAll load tests successful.".lightgreen
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment