Skip to content

Commit

Permalink
Cleanup CLI and start dockerized integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Arian Weber authored and Arian Weber committed Feb 8, 2024
1 parent 0d74e6c commit cf5a705
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 18 deletions.
54 changes: 54 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,57 @@ require "rspec/core/rake_task"
RSpec::Core::RakeTask.new(:spec)

task default: :spec

task :itest do
versions = [ "3.0", "2.6" ]
versions.each { |v| test_ruby_version(v) }
end


def test_ruby_version(version)

message = "Running tests on ruby v#{version}"
puts "#" * message.length
puts message
puts "#" * message.length

FileUtils.rm_rf(".pg-work")
FileUtils.mkdir(".pg-work")

# Generate docker file
dockerfile_path = File.join(".pg-work", "Dockerfile")
image_name = "pg-tools-test"
dockerfile = [ "FROM ruby:#{version}" ]
File.write(dockerfile_path, dockerfile.join("\n"))
sh "docker build --file #{dockerfile_path} --tag #{image_name} ."

test_cmd = []
test_cmd << "rm -f Gemfile.lock"
test_cmd << "bundle config set without packaging documentation"
test_cmd << "gem update --system --silent --no-document"
test_cmd << "bundle lock"
test_cmd << "bundle install --path /tmp/gems"
test_cmd << "ruby --version"
test_cmd << "bundle exec rspec"
test_cmd << "bundle exec rake build"
test_cmd << "gem install pkg/*"
test_cmd << "pg-tools --help"
test_cmd << "pg-tools doctor"
test_cmd = test_cmd.join(" && ")

docker_run = [ "docker run" ]
docker_run << "--rm"
docker_run << "--name pg-tools-container"
docker_run << "--mount type=bind,source='#{Dir.pwd}',target=/app"
docker_run << "--workdir /app"
docker_run << image_name
docker_run << "bash -c \"#{test_cmd}\""
docker_run = docker_run.join(" ")

sh docker_run

gem_file = Dir[File.join("pkg", "*.gem")].first
FileUtils.mkdir_p("out")
out_path = File.join("out", File.basename(gem_file).gsub(".gem", "-r#{version}.gem"))
mv gem_file, out_path
end
61 changes: 61 additions & 0 deletions doc/examples/railroad_crossing.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
####################################################################
# Model definition
####################################################################

model :RailroadCrossing do

####################################################################
# Train Components
####################################################################

# Train environment representing the actual physical train
graph :EnvTrain do
# velocity & position
end

# Position sensor of the train
graph :SensorTrainPosition do
states :idle, :send_close_msg, :send_is_closed_msg

end

graph :SensorTrainRadioModule do

end

# Actuator to
graph :ActTrainBrake do
states :idle, :active
end

graph :CtrlTrain do
end

####################################################################
# Crossing Components
####################################################################

graph :ActBarrierMotor do
var motor_speed: [-1, 0, 1], init: 0

states :idle, :opening, :closing


end

graph :CtrlCrossing do
end








graph :WingControl do

end


end
38 changes: 20 additions & 18 deletions lib/pg-tools/cli/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,16 @@ def self.select_components(only_arg, hide_arg, model)

class BaseCommand < Thor

desc "test", ""
desc "test", "Test the model specifications"
method_option :script, :type => :string
def test()
script_file = options[:script] || Settings.ruby_dsl.default_script_name
models = Interpret::PgScript.new.interpret(script_file)

models.each { |model|
results = NuSMV::Runner.new().run_specs(model)
results = Shell::LoadingPrompt.while_loading("Running specifications") {
NuSMV::Runner.new().run_specs(model)
}

results.each { |result|
stat_string = result.success ? "PASSED".c_success : "FAILED".c_error
Expand All @@ -109,7 +111,7 @@ def test()
}
end

desc "dcca", ""
desc "dcca", "Run the automatic DCCA for hazards of the model"
def dcca()
script = Interpret::PgScript.new
model = script.interpret('program-graph.rb')
Expand All @@ -128,25 +130,25 @@ def dcca()
}
end

desc "sim", ""
desc "sim", "Simulate the model and save each step as an image"
def sim()
script = Interpret::PgScript.new
model = script.interpret('program-graph.rb')

simulator = Simulation::Simulator.new(model)
states = simulator.run(steps: 5)
puts "Not implemented!"
# script = Interpret::PgScript.new
# model = script.interpret('program-graph.rb')

FileUtils.mkdir_p("video")
# simulator = Simulation::Simulator.new(model)
# states = simulator.run(steps: 5)

states.each_with_index.map { |state, index|
puml = Transform::PumlTransformation.new.transform_graph(model, variable_state: state)
puts puml
png = PlantumlBuilder::Formats::PNG.new(puml).load
File.binwrite("video/#{index}.png", png)
}
# FileUtils.mkdir_p("video")

puts states.map(&:to_s).join("\n---------\n")
# states.each_with_index.map { |state, index|
# puml = Transform::PumlTransformation.new.transform_graph(model, variable_state: state)
# puts puml
# png = PlantumlBuilder::Formats::PNG.new(puml).load
# File.binwrite("video/#{index}.png", png)
# }

# puts states.map(&:to_s).join("\n---------\n")
end

desc "init", "Initialize a new pg-tools project"
Expand Down Expand Up @@ -180,7 +182,7 @@ def init()

puts "Successfully initialized project at #{target.c_file}!"
puts "You can read the #{'README.md'.c_file} to get started."

puts "Run #{'pg-tools doctor'.c_blue} and follow the instructions to set up your environment!"
end

desc "doctor", "Check for common problems"
Expand Down

0 comments on commit cf5a705

Please sign in to comment.