forked from timcharper/gitty
/
sandbox_steps.rb
121 lines (98 loc) · 3.46 KB
/
sandbox_steps.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
World do
SandboxWorld.new
end
Before do
reset_sandbox!
end
When /^I switch to the directory "(.*)"$/ do |path|
path = (SandboxWorld::TMP_PATH + path).to_s
FileUtils.mkdir_p(path)
@current_dir = File.join(path)
end
Given /^I switch back to the original repository$/ do
@current_dir = SandboxWorld::SANDBOX_PATH.to_s
end
Given /^a file named "([^\"]*)"$/ do |file_name|
create_file(file_name, '')
end
Given /^a file named "([^\"]*)" with:$/ do |file_name, file_content|
create_file(file_name, file_content)
end
When /^the contents of "([^\"]*)" are changed to:$/ do |file_name, file_content|
create_file(file_name, file_content)
end
# the following code appears in "config/environment.rb" after /Rails::Initializer.run/:
Given /^the following code appears in "([^\"]*)" after \/([^\\\/]*)\/:$/ do |file_name, regex, content|
regex = Regexp.new(regex)
in_current_dir do
content_lines = File.read(file_name).split("\n")
0.upto(content_lines.length - 1) do |line_index|
if regex.match(content_lines[line_index])
content_lines.insert(line_index + 1, content)
break
end
end
File.open(file_name, 'wb') { |f| f << (content_lines * "\n") }
end
end
When /^I run "(.+)"$/ do |command|
run(command)
end
When /^I run:$/ do |command|
src_file = Tempfile.new('gitty_batch')
src_file.puts command
src_file.close
run("cat #{src_file.path} | bash")
end
Then /^the file "([^\"]*)" should include "([^\"]*)"$/ do |filename, content|
in_current_dir do
File.read(filename).should include(content)
end
end
Then /^the following (files|folders) should exist:$/ do |file_or_dir, table|
in_current_dir do
table.raw.map { |r| r.first}.each do |path|
next if File.exist?(path)
raise Spec::Expectations::ExpectationNotMetError, "expected #{path} to exist, but it didn't."
end
end
end
Then /^the following (files|folders) should not exist:$/ do |file_or_dir, table|
in_current_dir do
table.raw.map.each do |path|
File.exist?(path.first).should == false &&
File.symlink?(path.first).should == false
end
end
end
Then /^the (error output|output) should contain$/ do |which, text|
(which == "error output" ? last_stderr : last_stdout).should include(text)
end
Then /^the (error output|output) should contain "(.+)"$/ do |which, text|
(which == "error output" ? last_stderr : last_stdout).should include(text)
end
Then /^the (error output|output) should match \/(.+)\/$/ do |which, regex|
(which == "error output" ? last_stderr : last_stdout).should match(Regexp.new(regex))
end
Then /^the (error output|output) should not contain$/ do |which, text|
(which == "error output" ? last_stderr : last_stdout).should_not include(text)
end
Then /^the (error output|output) should not contain "(.+)"$/ do |which, text|
(which == "error output" ? last_stderr : last_stdout).should_not include(text)
end
Then /^the (error output|output) should be empty$/ do |which|
(which == "error output" ? last_stderr : last_stdout).should == ""
end
Then /^the (error output|output) should be$/ do |which, text|
(which == "error output" ? last_stderr : last_stdout).should == text
end
Then /^the last exit status should be (\d+)$/ do |code|
@last_exit_status.should == code.to_i
end
Given /^the file "(.+)" contains:$/ do |filename, content|
filename.gsub!("$GITTY_ASSETS", ENV["GITTY_ASSETS"])
in_current_dir do
FileUtils.mkdir_p(File.dirname(filename))
File.open(filename, "wb") { |f| f << content }
end
end