moss

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

cli_spec.rb (2923B)


      1 require "moss"
      2 
      3 cli = CLI.new
      4 class <<cli
      5   def usage_header
      6     "Store and retrieve encrypted secrets\n\n" +
      7       "Usage: moss [command] [parameters]...\n\n"
      8   end
      9 
     10   command :greet, "say hello" do |firstname:, lastname:|
     11     "hello, #{firstname} #{lastname}"
     12   end
     13   command :hang, "wait patiently" do |tight: false|
     14     #
     15   end
     16   command :encrypt, "scramble a file" do |file:, signed: false|
     17     #
     18   end
     19   link :scramble => :encrypt
     20   command :mail, "send email" do |rcpt:, subject: "(no subject)"|
     21     "/usr/lib/sendmail #{rcpt} -s #{subject.inspect}"
     22   end
     23   command :git, "perform a git operation" do |*command|
     24     command.join(":")
     25   end
     26   command :help, "show this message" do
     27     "there is no help"
     28   end
     29 end
     30 
     31 
     32 RSpec.describe CLI do
     33   describe "parameter parsing" do
     34 
     35     it "parses positional parameters" do
     36       expect(cli.parse_arguments %w(greet ringo starr)).
     37         to eq [:greet, {firstname: "ringo",
     38                         lastname: "starr"}]
     39     end
     40 
     41     it "parses valueless flags" do
     42       expect(cli.parse_arguments %w(hang --tight)).
     43         to match([:hang, have_key(:tight)])
     44       expect(cli.parse_arguments %w(hang)).
     45         to match([:hang, {}])
     46       expect(cli.parse_arguments %w(encrypt foo.txt --signed)).
     47         to match([:encrypt, { file: "foo.txt", signed: anything }])
     48       expect(cli.parse_arguments %w(encrypt foo.txt)).
     49         to match([:encrypt, { file: "foo.txt" }])
     50     end
     51 
     52     it "parses flags with values" do
     53       expect(cli.parse_arguments ["mail", "dan", "--subject=hello dan"]).
     54         to match([:mail, { rcpt: "dan", subject: "hello dan" }])
     55     end
     56 
     57     it "parses rest parameters" do
     58       expect(cli.parse_arguments ["git", "init", "--bare", "-mmain"]).
     59         to match([:git, ["init", "--bare", "-mmain"]])
     60     end
     61 
     62     it "parses when no parameters" do
     63       expect(cli.parse_arguments %w(help)).
     64         to match([:help, []])
     65     end
     66 
     67     it "processes aliases" do
     68       expect(cli.parse_arguments ["scramble", "foo.txt"]).
     69         to match([:encrypt, { file: "foo.txt" }])
     70     end
     71   end
     72 
     73   describe "dispatching" do
     74     it "calls the relevant command" do
     75       expect(cli.dispatch %w(greet jane eyre)).to eq "hello, jane eyre"
     76     end
     77 
     78     it "works for rest parameters" do
     79       expect(cli.dispatch %w(git init --bare))
     80         .to eq "init:--bare"
     81     end
     82 
     83     it "works for no parameters" do
     84       expect(cli.dispatch %w(help))
     85         .to eq "there is no help"
     86     end
     87 
     88     it "works for flags" do
     89       expect(cli.dispatch ["mail", "dan", "--subject=hello dan"])
     90         .to eq "/usr/lib/sendmail dan -s \"hello dan\""
     91     end
     92 
     93     it "complains if wrong params" do
     94       expect { cli.dispatch ["mail"] }
     95         .to raise_exception(CLI::UsageError, /usage: moss mail <rcpt> \[--subject\]/)
     96     end
     97   end
     98 
     99   describe "documentation" do
    100     subject { cli.usage }
    101 
    102     it { should match /send email/ }
    103     it { should match /wait patiently/ }
    104   end
    105 end