SimControl 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- SimControl (0.1.0)
4
+ SimControl (0.1.2)
5
5
  active_support
6
6
  i18n
7
7
  thor
@@ -15,7 +15,7 @@ GEM
15
15
  coderay (1.0.9)
16
16
  diff-lcs (1.2.4)
17
17
  fakefs (0.4.2)
18
- i18n (0.6.4)
18
+ i18n (0.6.5)
19
19
  method_source (0.8.1)
20
20
  pry (0.9.12.2)
21
21
  coderay (~> 1.0.5)
@@ -1,3 +1,5 @@
1
+ require "open3"
2
+
1
3
  module SimControl
2
4
  class PythonEnvironment < BaseEnvironment
3
5
  def initialize(script, args = {})
@@ -7,10 +9,25 @@ module SimControl
7
9
  raise "passing a virtualenv requires an interpreter" if @virtualenv and not @interpreter
8
10
  end
9
11
 
12
+ def script
13
+ @script
14
+ end
15
+
16
+ def args(scenario)
17
+ scenario.map { |k, v| "--#{ k } #{ v }" }.join " "
18
+ end
19
+
10
20
  def execute(scenario)
11
- args = scenario.map { |k, v| "--#{ k } #{ v }" }.join " "
12
- command = [interpreter, @script, args].reject(&:nil?).reject(&:empty?).join " "
13
- `#{ command }`
21
+ stdin, stout, stderr, thread = Open3.popen3 command(scenario), chdir: basedir
22
+ thread.join
23
+ end
24
+
25
+ def basedir
26
+ File.dirname @script
27
+ end
28
+
29
+ def command(scenario)
30
+ [interpreter, script, args(scenario)].reject(&:nil?).reject(&:empty?).join " "
14
31
  end
15
32
 
16
33
  def interpreter
@@ -1,3 +1,3 @@
1
1
  module SimControl
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -1,30 +1,48 @@
1
1
  require "spec_helper"
2
2
 
3
+ require "open3"
4
+
3
5
  describe SimControl::PythonEnvironment do
4
6
  let(:script) { "a-script" }
5
7
  describe "#simulate" do
8
+ it "uses popen3 to call the command and sets the cwd" do
9
+ command = "a-command"
10
+ basedir = "a-basedir"
11
+ thread = double("Thread")
12
+
13
+ simulation = SimControl::PythonEnvironment.new script
14
+ simulation.stub(:command).and_return(command)
15
+ simulation.stub(:basedir).and_return(basedir)
16
+ Open3.should_receive(:popen3).with(command, chdir: basedir).and_return([nil, nil, nil, thread])
17
+ thread.should_receive(:join)
18
+ simulation.execute(foo: 1)
19
+ end
20
+
6
21
  it "calls the script if nothing is passed in args" do
7
22
  simulation = SimControl::PythonEnvironment.new script
8
- simulation.should_receive(:`).with(script)
9
- simulation.execute({})
23
+ expect(simulation.script).to eq("a-script")
10
24
  end
11
25
 
12
26
  it "passes args to the script in -- syntax" do
13
27
  simulation = SimControl::PythonEnvironment.new script
14
- simulation.should_receive(:`).with("#{ script } --foo bar --baz 1")
15
- simulation.execute({foo: "bar", baz: 1})
28
+ expect(simulation.args({foo: "bar", baz: 1})).to eq("--foo bar --baz 1")
16
29
  end
17
30
 
18
31
  it "uses a given interpreter" do
19
32
  simulation = SimControl::PythonEnvironment.new script, interpreter: "pypy"
20
- simulation.should_receive(:`).with("pypy #{ script }")
21
- simulation.execute({})
33
+ expect(simulation.interpreter).to eq("pypy")
22
34
  end
23
35
 
24
36
  it "uses a given virtualenv and interpreter" do
25
37
  simulation = SimControl::PythonEnvironment.new script, virtualenv: "foo/bar", interpreter: "pypy"
26
- simulation.should_receive(:`).with("foo/bar/bin/pypy #{ script }")
27
- simulation.execute({})
38
+ expect(simulation.interpreter).to eq("foo/bar/bin/pypy")
39
+ end
40
+
41
+ it "composes the command" do
42
+ simulation = SimControl::PythonEnvironment.new script
43
+ simulation.stub(:args).and_return "--args 1"
44
+ simulation.stub(:interpreter).and_return "/foo/jpython"
45
+ expect(simulation.command(args: 1)).to eq("/foo/jpython a-script --args 1")
28
46
  end
29
47
 
30
48
  it "raised an exception is a virtualenv is passed but no interpreter" do
@@ -32,6 +50,11 @@ describe SimControl::PythonEnvironment do
32
50
  SimControl::PythonEnvironment.new script, virtualenv: "foo/bar"
33
51
  end.to raise_error /passing a virtualenv requires an interpreter/
34
52
  end
53
+
54
+ it "returns the basedir" do
55
+ simulation = SimControl::PythonEnvironment.new "/foo/bar/sim.py"
56
+ expect(simulation.basedir).to eq("/foo/bar")
57
+ end
35
58
  end
36
59
 
37
60
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: SimControl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-09 00:00:00.000000000 Z
12
+ date: 2013-08-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: thor