Module: Librarian::RSpec::Support::CliMacro

Defined in:
lib/librarian/rspec/support/cli_macro.rb

Defined Under Namespace

Classes: FakeShell, FileMatcher

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/librarian/rspec/support/cli_macro.rb', line 51

def self.included(base)
  base.instance_exec do
    let(:project_path) do
      project_path = Pathname.new(__FILE__).expand_path
      project_path = project_path.dirname until project_path.join("Rakefile").exist?
      project_path
    end
    let(:tmp) { project_path.join("tmp/spec/cli") }
    let(:pwd) { tmp + SecureRandom.hex(8) }

    before { tmp.mkpath }
    before { pwd.mkpath }

    after  { tmp.rmtree }
  end
end

Instance Method Details

#cli!(*args) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/librarian/rspec/support/cli_macro.rb', line 68

def cli!(*args)
  @shell = FakeShell.new
  @exit_status = Dir.chdir(pwd) do
    described_class.with_environment do
      described_class.returning_status do
        described_class.start args, :shell => @shell
      end
    end
  end
end

#exit_statusObject



105
106
107
# File 'lib/librarian/rspec/support/cli_macro.rb', line 105

def exit_status
  @exit_status
end

#have_file(rel_path, content = nil) ⇒ Object



109
110
111
# File 'lib/librarian/rspec/support/cli_macro.rb', line 109

def have_file(rel_path, content = nil)
  FileMatcher.new(rel_path, content)
end

#have_json_file(rel_path, content) ⇒ Object



113
114
115
# File 'lib/librarian/rspec/support/cli_macro.rb', line 113

def have_json_file(rel_path, content)
  FileMatcher.new(rel_path, content, :type => :json)
end

#shellObject



93
94
95
# File 'lib/librarian/rspec/support/cli_macro.rb', line 93

def shell
  @shell
end

#stderrObject



101
102
103
# File 'lib/librarian/rspec/support/cli_macro.rb', line 101

def stderr
  shell.stderr.string
end

#stdoutObject



97
98
99
# File 'lib/librarian/rspec/support/cli_macro.rb', line 97

def stdout
  shell.stdout.string
end

#strip_heredoc(text) ⇒ Object



89
90
91
# File 'lib/librarian/rspec/support/cli_macro.rb', line 89

def strip_heredoc(text)
  Librarian::Helpers.strip_heredoc(text)
end

#write_file!(path, content) ⇒ Object



79
80
81
82
83
# File 'lib/librarian/rspec/support/cli_macro.rb', line 79

def write_file!(path, content)
  path = pwd.join(path)
  path.dirname.mkpath
  path.open("wb"){|f| f.write(content)}
end

#write_json_file!(path, content) ⇒ Object



85
86
87
# File 'lib/librarian/rspec/support/cli_macro.rb', line 85

def write_json_file!(path, content)
  write_file! path, JSON.dump(content)
end