Skip to content

Commit

Permalink
Merge pull request #1518 from OpenC3/fix_specs
Browse files Browse the repository at this point in the history
Fix spec return code
  • Loading branch information
jmthomas authored Sep 26, 2024
2 parents c777b81 + 8bebed5 commit 54a56de
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 37 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,6 @@ Lint/UnusedMethodArgument:
# This rule doesn't allow for value.to_s(16)
Lint/RedundantStringCoercion:
Enabled: false
# This rule interferes with a lot of unit tests as well as json_accessor.rb
Lint/ConstantDefinitionInBlock:
Enabled: false
8 changes: 5 additions & 3 deletions openc3/lib/openc3/accessors/json_accessor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
require 'openc3/accessors/accessor'

# Monkey patch JsonPath to enable create_additions and allow_nan to support binary strings, and NaN, Infinity, -Infinity
class JsonPath
def self.process_object(obj_or_str, opts = {})
obj_or_str.is_a?(String) ? MultiJson.decode(obj_or_str, max_nesting: opts[:max_nesting], create_additions: true, allow_nan: true) : obj_or_str
OpenC3.disable_warnings do
class JsonPath
def self.process_object(obj_or_str, opts = {})
obj_or_str.is_a?(String) ? MultiJson.decode(obj_or_str, max_nesting: opts[:max_nesting], create_additions: true, allow_nan: true) : obj_or_str
end
end
end

Expand Down
5 changes: 5 additions & 0 deletions openc3/spec/config/config_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ module OpenC3
ConfigParser.progress_callback = nil
end

after(:each) do
ConfigParser.message_callback = nil
ConfigParser.progress_callback = nil
end

describe "parse_file", no_ext: true do
it "yields keyword, parameters to the block" do
tf = Tempfile.new('unittest')
Expand Down
6 changes: 6 additions & 0 deletions openc3/spec/microservices/interface_microservice_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,14 @@ class ApiTest
interface = im.instance_variable_get(:@interface)
interface.reconnect_delay = 0.1 # Override the reconnect delay to be quick

# Mock this because it calls exit which breaks SimpleCov
allow(OpenC3).to receive(:handle_fatal_exception) do |exception, _message|
expect(exception.message).to eql "test-error"
end

capture_io do |stdout|
Thread.new { im.run }

sleep 0.1 # Allow to start and immediately crash
expect(stdout.string).to include("RuntimeError")

Expand Down
4 changes: 2 additions & 2 deletions openc3/spec/microservices/router_microservice_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ApiTest
include Authorization
end

class TestInterface < Interface
class TestRouter < Interface
def initialize
super
@connected = false
Expand Down Expand Up @@ -82,7 +82,7 @@ def read_interface
interface = double("Interface").as_null_object
allow(interface).to receive(:connected?).and_return(true)
allow(System).to receive(:targets).and_return({ "INST" => interface })
model = RouterModel.new(name: "TEST_INT", scope: "DEFAULT", target_names: ["INST"], cmd_target_names: ["INST"], tlm_target_names: ["INST"], config_params: ["TestInterface"])
model = RouterModel.new(name: "TEST_INT", scope: "DEFAULT", target_names: ["INST"], cmd_target_names: ["INST"], tlm_target_names: ["INST"], config_params: ["TestRouter"])
model.create
model = MicroserviceModel.new(folder_name: "TEST", name: "DEFAULT__ROUTER__TEST_INT", scope: "DEFAULT", target_names: ["INST"])
model.create
Expand Down
15 changes: 0 additions & 15 deletions openc3/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,6 @@ def kill_leftover_threads
end
end

$system_exit_count = 0
# Overload exit so we know when it is called
alias old_exit exit
def exit(*args)
$system_exit_count += 1
end

RSpec.configure do |config|
# Enforce the new expect() syntax instead of the old should syntax
config.expect_with :rspec do |c|
Expand All @@ -325,14 +318,6 @@ def exit(*args)
$saved_stdout_const = Object.const_get(:STDOUT)
end

config.after(:all) do
OpenC3.disable_warnings do
def Object.exit(*args)
old_exit(*args)
end
end
end

# Before each test make sure $stdout and STDOUT are set. They might be messed
# up if a spec fails in the middle of capture_io and we don't have a chance
# to return and reset them.
Expand Down
26 changes: 9 additions & 17 deletions openc3/spec/top_level/top_level_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def self.cleanup_exceptions

describe "self.marshal_dump, self.marshal_load" do
it "dumps and load a Ruby object" do
capture_io do |stdout|
capture_io do |_stdout|
array = [1, 2, 3, 4]
OpenC3.marshal_dump('marshal_test', array)
array_load = OpenC3.marshal_load('marshal_test')
Expand All @@ -93,22 +93,18 @@ def self.cleanup_exceptions

it "rescues marshal dump errors" do
capture_io do |stdout|
system_exit_count = $system_exit_count
OpenC3.marshal_dump('marshal_test', Proc.new { '' })
expect($system_exit_count).to be > system_exit_count
expect { OpenC3.marshal_dump('marshal_test', Proc.new { '' }) }.to raise_error(SystemExit)
expect(stdout.string).to match("is defined for class Proc")
end
OpenC3.cleanup_exceptions()
end

it "rescues marshal dump errors in a Packet with a Mutex" do
capture_io do |stdout|
system_exit_count = $system_exit_count
pkt = Packet.new("TGT", "PKT")
pkt.append_item("ITEM", 16, :UINT)
pkt.read_all
OpenC3.marshal_dump('marshal_test', pkt)
expect($system_exit_count).to be > system_exit_count
expect { OpenC3.marshal_dump('marshal_test', pkt) }.to raise_error(SystemExit)
expect(stdout.string).to match("Mutex exists in a packet")
end
OpenC3.cleanup_exceptions()
Expand Down Expand Up @@ -139,7 +135,7 @@ def self.cleanup_exceptions
describe "run_process" do
it "returns a Thread" do
if Kernel.is_windows?
capture_io do |stdout|
capture_io do |_stdout|
thread = OpenC3.run_process("ping 127.0.0.1 -n 2 -w 1000 > nul")
sleep 0.1
expect(thread).to be_a Thread
Expand All @@ -164,10 +160,10 @@ def self.cleanup_exceptions
end

describe "hash_files" do
xit "calculates a hashing sum across files in md5 mode" do
it "calculates a hashing sum across files in md5 mode" do
File.open(File.join(OpenC3::PATH, 'test1.txt'), 'w') { |f| f.puts "test1" }
File.open(File.join(OpenC3::PATH, 'test2.txt'), 'w') { |f| f.puts "test2" }
digest = OpenC3.hash_files(["test1.txt", "test2.txt"])
digest = OpenC3.hash_files(["test1.txt", "test2.txt"], nil, 'MD5')
expect(digest.digest.length).to be 16
expect(digest.hexdigest).to eql 'e51dfbea83de9c7e6b49560089d8a170'
File.delete(File.join(OpenC3::PATH, 'test1.txt'))
Expand Down Expand Up @@ -207,7 +203,7 @@ def self.cleanup_exceptions
# Move the defaults output dir out of the way for this test
begin
FileUtils.mv('outputs', 'outputs_bak')
rescue => err
rescue => e
Dir.entries('outputs/logs').each do |entry|
next if entry[0] == '.'

Expand All @@ -217,7 +213,7 @@ def self.cleanup_exceptions
STDOUT.puts entry
end
end
raise err
raise e
end

# Create a logs directory as the first order backup
Expand Down Expand Up @@ -251,9 +247,7 @@ def self.cleanup_exceptions
describe "handle_fatal_exception" do
it "writes to the Logger and exit" do
capture_io do |stdout|
system_exit_count = $system_exit_count
OpenC3.handle_fatal_exception(RuntimeError.new)
expect($system_exit_count).to eql(system_exit_count + 1)
expect { OpenC3.handle_fatal_exception(RuntimeError.new) }.to raise_error(SystemExit)
expect(stdout.string).to match("Fatal Exception! Exiting...")
end
OpenC3.cleanup_exceptions()
Expand All @@ -263,9 +257,7 @@ def self.cleanup_exceptions
describe "handle_critical_exception" do
it "writes to the Logger" do
capture_io do |stdout|
system_exit_count = $system_exit_count
OpenC3.handle_critical_exception(RuntimeError.new)
expect($system_exit_count).to eql(system_exit_count)
expect(stdout.string).to match("Critical Exception!")
end
OpenC3.cleanup_exceptions()
Expand Down

0 comments on commit 54a56de

Please sign in to comment.