Is there a way to mock Facter::Core::Execution.execute output in ruby? - ruby

I have a custom fact in ruby that uses Facter::Core::Execution.execute to execute a system command and sets the fact to true or false based on the output of the execute command. Is there a way I could mock the output of the execute command to test it?

Using rspec:
expect(Facter::Core::Execution).to receive(:execute).and_return(true)

Related

Execute commands and print their output

Is there a way in ruby to execute a command line utility and have its output displayed in real time, something like myrubyscript rspec, where myrubyscript runs rspec and immediately prints output as it receives it from rspec?
Currently, if I invoke rspec with backticks or system, I don't see the rspec output immediately. Rather, it prints at the end.
Preferably, I want the solution to have block form, so that I run code before and after I exec the passed argument.
Use Open3.popen3.
require "open3"
Open3.popen3...

Running `cucumber feature/test.feature` in ruby shows weird behaviour

#!/usr/bin/ruby
`cucumber feature/test.feature`
running the above code issue lots of cucumber feature/test.feature commands.. why ?
When i see processes list there are 30 to 50 processes running cucumber command
also ruby program never terminates
Try running your feature files from outside the 'features' folder. Think this will solve the issue.(tested using the command line)
User:project user$ ls
features
User:project user$cucumber example.feature
The first line instructs the shell to run myapp.rb, which is, AFAIU, this script itself. Namely, each execution of the script recursively runs itself again.
Try the following:
#!/usr/bin/ruby
`cucumber feature/test.feature`
Or, even better, directly from the CLI:
cucumber feature/test.feature
To run all the tests simply issue the cucumber command without args:
cucumber

Executing particular tests under ruby test framework

I have a set of test cases under ruby test framework 1.8.7
Lets say i have a ruby file named check.rb which contains differents tests like
test_a_check, test_b_check and test_c_check.
Now when i run the file ruby check.rb, all the test cases will be executed.
My part of the question is,
I want to pass a new parameter to the script while running, say ruby check.rb --sunset
based on the sunset parameter i want my script to execute only test_a_check and test_b_check and not the test_c_check.
By default, if i run the script all the tests should be excuted but when the --sunset parameter is passed only two of three tests should be executed.
is there are way i can achieve this?
If you are using minitest you can specify the method via
ruby check --name test_method_name
If it's a common testing framework, then look into it's manual, but
If it's your personal testing script, then just look in ARGV:
test_a_check
test_b_check
test_c_check if ARGV[0] != '--sunset'

How can I make RSpec output to console when run as a command %x[rspec] from Ruby script?

I have a class with an instance method that runs RSpec using the %x[] notation:
class TestRunner
def run_rspec
# do stuff
%x[rspec spec -c -f documentation]
# do more stuff
end
end
When I do this:
> tr = TestRunner.new
> tr.run_rspec
The documentation (group and example names) does not appear in the console.
To contrast, when I run rspec straight from the command line I get this:
$ rspec spec -c -f documentation
a group name
an example
another example
...
I don't want to do this:
puts %x[rspec spec -c -f documentation
Because then the output all spits out in one huge clump at the very end. I want it to run in "real time," with each example showing up as each test is run.
Is there a way, with the setup I have, to get RSpec to announce what it's doing, as it's doing it (as it does when run normally from the command line)?
I've been advised that system() and the other shell methods can be dangerous to use, so I've opted to switch to the even-better approach of using RSpec itself:
RSpec::Core::Runner.run(['spec', '-c', '-f', 'documentation'])
rather than calling it via shell from my Ruby script.
Ruby offers several options for running programs from the command line. I was using %x[], the wrong choice for my use case.
Solution: Use system(), not %x[] -- rspec will write to STDOUT in real-time when I call it with system('rspec spec').
Some background in case it's helpful to anyone who stumbles upon this question:
Consider the differences between Ruby's command-line options:
%x[command] accumulates the result of command and returns it, in one chunk.
exec('command') will output command as command runs, but will replace whatever process called it -- i.e., if you use exec in your Ruby script, your Ruby script won't finish.
system('command') executes command in a subshell, and returns to the calling script.
This is why system was the choice for my script.

is it possible to pass through command-line options to a script in rspec

I have an RSpec script that tests a program in a different language I am developing. Since I can run and test 32 and 64-bit versions of this application, I would like to have a way to signal this on the command-line.
What I really want is to do something like this:
rspec -c myspec.rb lin32
or
rspec -c myspec.rb lin64
and have the lin32 or lin64 be passed as a string I can access in the ruby file itself. Is this possible? This site mentions environment variables but that is cumbersome. It also mentioned doing ARGV manipulation -- is this a possible way of doing it?
From David Chelimsky
You can't pass arbitrary arguments to the rspec command, but you can set an environment variable like this:
SLEEP=10 rspec test.rb
Then within the script, the value of ENV["SLEEP"] is "10", so you can say:
sleep(ENV["SLEEP"].to_f)
Try the -- parameter. It's used to tell an app to stop processing parameters and pass the remaining ones to a child process. I don't know if rspec understands it but it's worth a try.
`rspec -c myspec.rb -- lin32`
`rspec -c myspec.rb -- lin64`

Resources