forked from sds/overcommit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell_helpers.rb
45 lines (39 loc) · 1.17 KB
/
shell_helpers.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
# frozen_string_literal: true
require 'timeout'
require 'overcommit/subprocess'
# Helpers for executing shell commands in tests.
module ShellHelpers
def shell(command)
Overcommit::Subprocess.spawn(command)
end
def symlink(source, dest)
Overcommit::Utils::FileUtils.symlink(source, dest)
end
def touch(file)
FileUtils.touch(file)
end
# Wait until the specified condition is true or the given timeout has elapsed,
# whichever comes first.
#
# @param options [Hash]
# @raise [Timeout::TimeoutError] timeout has elapsed before condition holds
def wait_until(options = {})
Timeout.timeout(options.fetch(:timeout) { 1 }) do
loop do
return if yield
sleep options.fetch(:check_interval) { 0.1 }
end
end
end
# Output text to file using `File#puts`, which mimics the behavior of the
# `echo` shell command.
#
# @param text [String] text to write
# @param file [String] path to target file
# @param options [Hash]
# @option options [Boolean] :append whether to append to existing file
def echo(text, file, options = {})
mode = options[:append] ? 'a' : 'w'
File.open(file, mode) { |f| f.puts(text) }
end
end