Skip to content

Commit

Permalink
Merge pull request #5184 from NREL/fix_tests
Browse files Browse the repository at this point in the history
Fix tests
  • Loading branch information
jmarrec authored May 3, 2024
2 parents e7099d2 + 05ca3f6 commit d671778
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 97 deletions.
65 changes: 44 additions & 21 deletions ruby/engine/test/RubyEngine_GTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class RubyEngineFixture : public testing::Test
(*thisEngine)->registerType<openstudio::measure::ModelMeasure*>("openstudio::measure::ModelMeasure *");
(*thisEngine)->exec("OpenStudio::init_rest_of_openstudio()");
}

// tear down after each test
virtual void TearDown() override {
// Want to ensure the engine is reset regardless of the test outcome, or it may throw a segfault
Expand All @@ -56,10 +57,13 @@ class RubyEngineFixture : public testing::Test
};

TEST_F(RubyEngineFixture, BadMeasure) {
::testing::FLAGS_gtest_death_test_style = "threadsafe";

const std::string classAndDirName = "BadMeasure";
ASSERT_DEATH(
{
const std::string classAndDirName = "BadMeasure";

const auto scriptPath = getScriptPath(classAndDirName);
const auto scriptPath = getScriptPath(classAndDirName);
auto measureScriptObject = (*thisEngine)->loadMeasure(scriptPath, classAndDirName);
auto* measurePtr = (*thisEngine)->getAs<openstudio::measure::ModelMeasure*>(measureScriptObject);

Expand All @@ -70,23 +74,31 @@ TEST_F(RubyEngineFixture, BadMeasure) {
Traceback (most recent call last):
{0}:12:in `another_method'
{0}:16:in `arguments')",
scriptPath.generic_string());
scriptPath.generic_string());

openstudio::model::Model model;
try {
measurePtr->arguments(model);
ASSERT_FALSE(true) << "Expected measure arguments(model) to throw";
} catch (std::exception& e) {
std::string error = e.what();
EXPECT_EQ(expected_exception, error);
}
openstudio::model::Model model;

try {
measurePtr->arguments(model);
ASSERT_FALSE(true) << "Expected measure arguments(model) to throw";
} catch (std::exception& e) {
std::string error = e.what();
EXPECT_EQ(expected_exception, error);
}

thisEngine->reset();
},
"oops");
}

TEST_F(RubyEngineFixture, WrongMethodMeasure) {
::testing::FLAGS_gtest_death_test_style = "threadsafe";

const std::string classAndDirName = "WrongMethodMeasure";
ASSERT_DEATH(
{
const std::string classAndDirName = "WrongMethodMeasure";

const auto scriptPath = getScriptPath(classAndDirName);
const auto scriptPath = getScriptPath(classAndDirName);
auto measureScriptObject = (*thisEngine)->loadMeasure(scriptPath, classAndDirName);
auto* measurePtr = (*thisEngine)->getAs<openstudio::measure::ModelMeasure*>(measureScriptObject);

Expand All @@ -104,16 +116,24 @@ Traceback (most recent call last):
measurePtr->arguments(model);
ASSERT_FALSE(true) << "Expected measure arguments(model) to throw";
} catch (std::exception& e) {
std::string error = e.what();
EXPECT_EQ(expected_exception, stripAddressFromErrorMessage(error));
}
std::string error = e.what();
EXPECT_EQ(expected_exception, stripAddressFromErrorMessage(error));
}

thisEngine->reset();
},
"undefined method `nonExistingMethod'");
}

TEST_F(RubyEngineFixture, StackLevelTooDeepMeasure) {

const std::string classAndDirName = "StackLevelTooDeepMeasure";
::testing::FLAGS_gtest_death_test_style = "threadsafe";

ASSERT_DEATH(
{
const std::string classAndDirName = "StackLevelTooDeepMeasure";

const auto scriptPath = getScriptPath(classAndDirName);
const auto scriptPath = getScriptPath(classAndDirName);
auto measureScriptObject = (*thisEngine)->loadMeasure(scriptPath, classAndDirName);
auto* measurePtr = (*thisEngine)->getAs<openstudio::measure::ModelMeasure*>(measureScriptObject);

Expand Down Expand Up @@ -149,8 +169,11 @@ Traceback (most recent call last):
measurePtr->arguments(model);
ASSERT_FALSE(true) << "Expected measure arguments(model) to throw";
} catch (std::exception& e) {
std::string error = e.what();
EXPECT_EQ(expected_exception, stripNumLevels(error));
}
std::string error = e.what();
EXPECT_EQ(expected_exception, stripNumLevels(error));
}
thisEngine->reset();
},
"stack level too deep");
}

31 changes: 13 additions & 18 deletions src/cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,23 +100,18 @@ int main(int argc, char* argv[]) {

app.get_formatter()->column_width(35);

auto* const verboseOpt = app
.add_flag_function(
"--verbose",
[](auto count) {
if (count == 1) {
openstudio::Logger::instance().standardOutLogger().setLogLevel(LogLevel::Debug);
LOG_FREE(Debug, "openstudio.CLI",
"Setting Log Level to Debug"
<< "(" << LogLevel::Debug << ")");
} else if (count == 2) {
openstudio::Logger::instance().standardOutLogger().setLogLevel(LogLevel::Trace);
LOG_FREE(Debug, "openstudio.CLI",
"Setting Log Level to Trace"
<< "(" << LogLevel::Trace << ")");
}
},
"Print the full log to STDOUT - sets verbosity to Debug if given once and Trace if given twice.");
auto* const verboseOpt = app.add_flag_function(
"--verbose",
[](auto count) {
if (count == 1) {
openstudio::Logger::instance().standardOutLogger().setLogLevel(LogLevel::Debug);
LOG_FREE(Debug, "openstudio.CLI", "Setting Log Level to Debug (" << LogLevel::Debug << ")");
} else if (count == 2) {
openstudio::Logger::instance().standardOutLogger().setLogLevel(LogLevel::Trace);
LOG_FREE(Debug, "openstudio.CLI", "Setting Log Level to Trace (" << LogLevel::Trace << ")");
}
},
"Print the full log to STDOUT - sets verbosity to Debug if given once and Trace if given twice.");

// specify string->value mappings
const std::map<std::string, LogLevel> logLevelMap{
Expand All @@ -131,7 +126,7 @@ int main(int argc, char* argv[]) {
[](const LogLevel& level) {
const auto loglLevelStr = logLevelStrs[static_cast<size_t>(level) - static_cast<size_t>(LogLevel::Trace)];
openstudio::Logger::instance().standardOutLogger().setLogLevel(level);
LOG_FREE(Debug, "openstudio.CLI", "Setting Log Level to " << loglLevelStr << "(" << level << ")");
LOG_FREE(Debug, "openstudio.CLI", "Setting Log Level to " << loglLevelStr << " (" << level << ")");
},
"LogLevel settings: One of {Trace, Debug, Info, Warn, Error, Fatal} [Default: Warn] Excludes: --verbose")
->excludes(verboseOpt)
Expand Down
3 changes: 2 additions & 1 deletion src/cli/test/bundle_git/Gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
source 'http://rubygems.org'

gem 'xml-simple'
gem 'openstudio-extension', git: 'https://github.com/NREL/openstudio-extension-gem.git', ref: 'v0.4.0'
# gem 'openstudio-extension', git: 'https://github.com/NREL/openstudio-extension-gem.git', ref: 'v0.8.0'
gem 'simplecov', git: 'https://github.com/simplecov-ruby/simplecov', ref: 'v0.21.2'
gem 'tilt', git: 'https://github.com/rtomayko/tilt.git', ref: 'abe77eaf1b5f8da0a7e46135f'
36 changes: 23 additions & 13 deletions src/cli/test/bundle_git/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,30 @@ def local_gems
[name, specs.map{ |spec| spec.version.to_s }.join(',')].join(' ')
}

# test a github checkout gem
require 'openstudio'
require 'openstudio/extension'
puts OpenStudio::Extension::VERSION
raise "OpenStudio Extension version does not match" unless OpenStudio::Extension::VERSION.to_s == '0.4.0'

# A gem we do not embed
require 'tilt'
puts Tilt::VERSION
raise "Tilt version does not match" unless Tilt::VERSION == '2.0.8'

did_fail = false
begin
require 'openstudio-standards'
rescue LoadError
did_fail = true
end
raise "Should allow to load the embedded openstudio-standards" unless did_fail == false
# A gem we do embed, different version
require 'simplecov'
puts SimpleCov::VERSION
raise "SimpleCov version does not match" unless SimpleCov::VERSION.to_s == '0.21.2'

# TODO: until we have gems on rubygems, this is unpractical without forcing a
# download from git for all openstudio-extension, measure-tester, and workflow
# gems

# # test a github checkout gem
# require 'openstudio'
# require 'openstudio/extension'
# puts OpenStudio::Extension::VERSION
# raise "OpenStudio Extension version does not match" unless OpenStudio::Extension::VERSION.to_s == '0.8.0'

# did_fail = false
# begin
# require 'openstudio-standards'
# rescue LoadError
# did_fail = true
# end
# raise "Should allow to load the embedded openstudio-standards" unless did_fail == false
2 changes: 1 addition & 1 deletion src/cli/test/run_minitest_fail.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def run_file(
)

r = subprocess.run(
[str(args.ruby_or_cli_path), "-e", "at exit { exit 12 }"],
[str(args.ruby_or_cli_path), "-e", "at_exit { exit 12 }"],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf-8", universal_newlines=True
)
assert r.returncode == 12
55 changes: 27 additions & 28 deletions src/cli/test/test_embedded_help.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,63 +11,62 @@ def test_dir_glob
Dir.chdir(File.join(File.dirname(__FILE__), '..'))

# test things that should work in ruby or CLI
no_block_glob = Dir["*.txt", "*.rb"]
no_block_glob = Dir["*.txt", "*.cpp"]
assert_instance_of(Array, no_block_glob)
assert_includes(no_block_glob, 'CMakeLists.txt')
assert_includes(no_block_glob, 'embedded_help.rb')
puts no_block_glob.index('test_embedded_help.rb')
assert_nil(no_block_glob.index('test_embedded_help.rb'))
assert_includes(no_block_glob, 'main.cpp')
assert_nil(no_block_glob.index('test_main.cpp'))

no_block_glob = Dir.glob(["*.txt", "*.rb"])
no_block_glob = Dir.glob(["*.txt", "*.cpp"])
assert_instance_of(Array, no_block_glob)
assert_includes(no_block_glob, 'CMakeLists.txt')
assert_includes(no_block_glob, 'embedded_help.rb')
assert_nil(no_block_glob.index('test_embedded_help.rb'))
assert_includes(no_block_glob, 'main.cpp')
assert_nil(no_block_glob.index('test_main.cpp'))

no_block_glob = Dir.glob("*.txt")
assert_instance_of(Array, no_block_glob)
assert_includes(no_block_glob, 'CMakeLists.txt')
assert_nil(no_block_glob.index('embedded_help.rb'))
assert_nil(no_block_glob.index('test_embedded_help.rb'))
assert_nil(no_block_glob.index('main.cpp'))
assert_nil(no_block_glob.index('test_main.cpp'))

no_block_glob = Dir["**/*.txt", "**/*.rb"]
no_block_glob = Dir["**/*.txt", "**/*.cpp"]
assert_instance_of(Array, no_block_glob)
assert_includes(no_block_glob, 'CMakeLists.txt')
assert_includes(no_block_glob, 'embedded_help.rb')
assert_includes(no_block_glob, 'test/test_embedded_help.rb')
assert_includes(no_block_glob, 'main.cpp')
assert_includes(no_block_glob, 'test/Logger_GTest.cpp')

no_block_glob = Dir["*{.txt,.rb}"]
no_block_glob = Dir["*{.txt,.cpp}"]
assert_instance_of(Array, no_block_glob)
assert_includes(no_block_glob, 'CMakeLists.txt')
assert_includes(no_block_glob, 'embedded_help.rb')
assert_nil(no_block_glob.index('test_embedded_help.rb'))
assert_nil(no_block_glob.index('test/test_embedded_help.rb'))
assert_includes(no_block_glob, 'main.cpp')
assert_nil(no_block_glob.index('test_main.cpp'))
assert_nil(no_block_glob.index('test/Logger_GTest.cpp'))

no_block_glob = Dir["{,*,*/*}.{txt,rb}"]
no_block_glob = Dir["{,*,*/*}.{txt,cpp}"]
assert_instance_of(Array, no_block_glob)
assert_includes(no_block_glob, 'CMakeLists.txt')
assert_includes(no_block_glob, 'embedded_help.rb')
assert_includes(no_block_glob, 'test/test_embedded_help.rb')
assert_includes(no_block_glob, 'main.cpp')
assert_includes(no_block_glob, 'test/Logger_GTest.cpp')

no_block_glob = Dir.glob("{,*,*/*}.{txt,rb}")
no_block_glob = Dir.glob("{,*,*/*}.{txt,cpp}")
assert_instance_of(Array, no_block_glob)
assert_includes(no_block_glob, 'CMakeLists.txt')
assert_includes(no_block_glob, 'embedded_help.rb')
assert_includes(no_block_glob, 'test/test_embedded_help.rb')
assert_includes(no_block_glob, 'main.cpp')
assert_includes(no_block_glob, 'test/Logger_GTest.cpp')

no_block_glob = Dir["./**/*.txt", "./**/*.rb"]
no_block_glob = Dir["./**/*.txt", "./**/*.cpp"]
assert_instance_of(Array, no_block_glob)
assert_includes(no_block_glob, './CMakeLists.txt')
assert_includes(no_block_glob, './embedded_help.rb')
assert_includes(no_block_glob, './test/test_embedded_help.rb')
assert_includes(no_block_glob, './main.cpp')
assert_includes(no_block_glob, './test/Logger_GTest.cpp')

block_glob = []
Dir["*.txt", "*.rb"].each do |p|
Dir["*.txt", "*.cpp"].each do |p|
block_glob << p
end
assert_includes(block_glob, 'CMakeLists.txt')
assert_includes(block_glob, 'embedded_help.rb')
assert_nil(block_glob.index('test_embedded_help.rb'))
assert_includes(block_glob, 'main.cpp')
assert_nil(block_glob.index('test_main.cpp'))

assert(File.fnmatch( "C:/test/help.rb", "C:/test/help.rb", 0))
assert(File.fnmatch( "C:/test/help.rb", "C:/test/help.rb", File::FNM_EXTGLOB))
Expand Down
13 changes: 1 addition & 12 deletions src/cli/test/test_embedded_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,6 @@ def test_rmd160
assert(true)
end

def test_sdbm
require 'sdbm'

SDBM.open 'my_database' do |db|
db['apple'] = 'fruit'
db['pear'] = 'fruit'
db['carrot'] = 'vegetable'
db['tomato'] = 'vegetable'
end
assert(true)
end

def test_sha1
require 'digest/sha1'
s = Digest::SHA1.hexdigest 'abc'
Expand Down Expand Up @@ -234,6 +222,7 @@ def test_msgpack
end

def test_json_schemer
skip("Disabled due to unf_ext, see https://github.com/NREL/openstudio-gems/issues/72")
require 'json_schemer'

schema = {
Expand Down
3 changes: 2 additions & 1 deletion src/cli/test/test_loglevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def test_loglevel(osclipath: Path, language: str, logLevel: str):
args += ["-c", PYTHON_PROGRAM]
lines = subprocess.check_output(args, encoding="utf-8").splitlines()
lines = remove_classic_box(lines)
assert f"Setting Log Level to {logLevel} ({loglevel_int_value})" in lines
if logLevel in ['Trace', 'Debug']:
assert f"[openstudio.CLI] <-2> Setting Log Level to {logLevel} ({loglevel_int_value})" in lines
for i, msgLevel in enumerate(LOG_LEVELS):
msg = f"[test] <{get_loglevel_int_value(msgLevel)}> {msgLevel}"

Expand Down
4 changes: 2 additions & 2 deletions src/workflow/RunEnergyPlus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ void OSWorkflow::runEnergyPlus() {
LOG(Info, "Running command '" << cmd << "'");

int result = 0;
detailedTimeBlock("Running ExpandObjects", [this, /*&cmd,*/ &result, &runDirPath, &runDirResults, &stdout_ofs] {
detailedTimeBlock("Running ExpandObjects", [this, /*&cmd,*/ &result, &runDirResults, &stdout_ofs] {
// result = std::system(cmd.c_str());
namespace bp = boost::process;
bp::ipstream is;
Expand Down Expand Up @@ -217,7 +217,7 @@ void OSWorkflow::runEnergyPlus() {
int result = 0;

if constexpr (useBoostProcess) {
detailedTimeBlock("Running EnergyPlus", [this, /*&cmd,*/ &result, &runDirPath, &runDirResults, &inIDF, &stdout_ofs] {
detailedTimeBlock("Running EnergyPlus", [this, /*&cmd,*/ &result, &runDirResults, &inIDF, &stdout_ofs] {
// result = std::system(cmd.c_str());
namespace bp = boost::process;
bp::ipstream is;
Expand Down

0 comments on commit d671778

Please sign in to comment.