summaryrefslogtreecommitdiff
path: root/spec/unit/face/module/search_spec.rb
blob: d826a3ca9319395b056621fa2169b2c420298951 (plain)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
require 'spec_helper'
require 'puppet/face'
require 'puppet/application/module'
require 'puppet/module_tool'

describe "puppet module search" do
  subject { Puppet::Face[:module, :current] }

  let(:options) do
    {}
  end

  describe Puppet::Application::Module do
    subject do
      app = Puppet::Application::Module.new
      app.stubs(:action).returns(Puppet::Face.find_action(:module, :search))
      app
    end

    before { subject.render_as = :console }
    before { Puppet::Util::Terminal.stubs(:width).returns(100) }

    it 'should output nothing when receiving an empty dataset' do
      subject.render({:answers => [], :result => :sucess}, ['apache', {}]).should == "No results found for 'apache'."
    end

    it 'should return error and exit when error returned' do
      results = {
        :result => :failure,
        :error => {
          :oneline => 'Something failed',
          :multiline => 'Something failed',
        }
      }
      expect { subject.render(results, ['apache', {}]) }.to raise_error 'Something failed'
    end

    it 'should output a header when receiving a non-empty dataset' do
      results = {
        :result => :success,
        :answers => [
          {'full_name' => '', 'author' => '', 'desc' => '', 'tag_list' => [] },
        ],
      }

      subject.render(results, ['apache', {}]).should =~ /NAME/
      subject.render(results, ['apache', {}]).should =~ /DESCRIPTION/
      subject.render(results, ['apache', {}]).should =~ /AUTHOR/
      subject.render(results, ['apache', {}]).should =~ /KEYWORDS/
    end

    it 'should output the relevant fields when receiving a non-empty dataset' do
      results = {
        :result => :success,
        :answers => [
          {'full_name' => 'Name', 'author' => 'Author', 'desc' => 'Summary', 'tag_list' => ['tag1', 'tag2'] },
        ]
      }

      subject.render(results, ['apache', {}]).should =~ /Name/
      subject.render(results, ['apache', {}]).should =~ /Author/
      subject.render(results, ['apache', {}]).should =~ /Summary/
      subject.render(results, ['apache', {}]).should =~ /tag1/
      subject.render(results, ['apache', {}]).should =~ /tag2/
    end

    it 'should elide really long descriptions' do
      results = {
        :result => :success,
        :answers => [
          {
            'full_name' => 'Name',
            'author' => 'Author',
            'desc' => 'This description is really too long to fit in a single data table, guys -- we should probably set about truncating it',
            'tag_list' => ['tag1', 'tag2'],
          },
        ]
      }

      subject.render(results, ['apache', {}]).should =~ /\.{3}  @Author/
    end

    it 'should never truncate the module name' do
      results = {
        :result => :success,
        :answers => [
          {
            'full_name' => 'This-module-has-a-really-really-long-name',
            'author' => 'Author',
            'desc' => 'Description',
            'tag_list' => ['tag1', 'tag2'],
          },
        ]
      }

      subject.render(results, ['apache', {}]).should =~ /This-module-has-a-really-really-long-name/
    end

    it 'should never truncate the author name' do
      results = {
        :result => :success,
        :answers => [
          {
            'full_name' => 'Name',
            'author' => 'This-author-has-a-really-really-long-name',
            'desc' => 'Description',
            'tag_list' => ['tag1', 'tag2'],
          },
        ]
      }

      subject.render(results, ['apache', {}]).should =~ /@This-author-has-a-really-really-long-name/
    end

    it 'should never remove tags that match the search term' do
      results = {
        :results => :success,
        :answers => [
          {
            'full_name' => 'Name',
            'author' => 'Author',
            'desc' => 'Description',
            'tag_list' => ['Supercalifragilisticexpialidocious'] + (1..100).map { |i| "tag#{i}" },
          },
        ]
      }

      subject.render(results, ['Supercalifragilisticexpialidocious', {}]).should =~ /Supercalifragilisticexpialidocious/
      subject.render(results, ['Supercalifragilisticexpialidocious', {}]).should_not =~ /tag/
    end

    {
      100 => "NAME          DESCRIPTION                                     AUTHOR         KEYWORDS#{' '*15}\n"\
             "Name          This description is really too long to fit ...  @JohnnyApples  tag1 tag2 taggitty3#{' '*4}\n",

      70  => "NAME          DESCRIPTION                 AUTHOR         KEYWORDS#{' '*5}\n"\
             "Name          This description is rea...  @JohnnyApples  tag1 tag2#{' '*4}\n",

      80  => "NAME          DESCRIPTION                        AUTHOR         KEYWORDS#{' '*8}\n"\
             "Name          This description is really too...  @JohnnyApples  tag1 tag2#{' '*7}\n",

      200 => "NAME          DESCRIPTION                                                                                                        AUTHOR         KEYWORDS#{' '*48}\n"\
             "Name          This description is really too long to fit in a single data table, guys -- we should probably set about trunca...  @JohnnyApples  tag1 tag2 taggitty3#{' '*37}\n"
    }.each do |width, expectation|
      it "should resize the table to fit the screen, when #{width} columns" do
        results = {
          :result => :success,
          :answers => [
            {
              'full_name' => 'Name',
              'author' => 'JohnnyApples',
              'desc' => 'This description is really too long to fit in a single data table, guys -- we should probably set about truncating it',
              'tag_list' => ['tag1', 'tag2', 'taggitty3'],
            },
          ]
        }

        Puppet::Util::Terminal.expects(:width).returns(width)
        result = subject.render(results, ['apache', {}])
        result.lines.sort_by(&:length).last.chomp.length.should <= width
        result.should == expectation
      end
    end
  end

  describe "option validation" do
    context "without any options" do
      it "should require a search term" do
        pattern = /wrong number of arguments/
        expect { subject.search }.to raise_error ArgumentError, pattern
      end
    end

    it "should accept the --module-repository option" do
      forge = mock("Puppet::Forge")
      searcher = mock("Searcher")
      options[:module_repository] = "http://forge.example.com"

      Puppet::Forge.expects(:new).with().returns(forge)
      Puppet::ModuleTool::Applications::Searcher.expects(:new).with("puppetlabs-apache", forge, has_entries(options)).returns(searcher)
      searcher.expects(:run)

      subject.search("puppetlabs-apache", options)
    end
  end

  describe "inline documentation" do
    subject { Puppet::Face[:module, :current].get_action :search }

    its(:summary)     { should =~ /search.*module/im }
    its(:description) { should =~ /search.*module/im }
    its(:returns)     { should =~ /array/i }
    its(:examples)    { should_not be_empty }

    %w{ license copyright summary description returns examples }.each do |doc|
      context "of the" do
        its(doc.to_sym) { should_not =~ /(FIXME|REVISIT|TODO)/ }
      end
    end
  end
end