hackpad-cli 0.0.7 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'spec_helper'
4
- require "hackpad/cli/config"
4
+ require 'hackpad/cli/config'
5
5
 
6
6
  describe Hackpad::Cli::Config do
7
7
 
@@ -10,21 +10,22 @@ describe Hackpad::Cli::Config do
10
10
  let(:options) { { configdir: configdir, workspace: 'default' } }
11
11
 
12
12
  before :each do
13
- FileUtils.mkdir_p configdir unless Dir.exists?(configdir)
13
+ FileUtils.mkdir_p configdir unless Dir.exist?(configdir)
14
14
  end
15
15
 
16
16
  after :each do
17
- FileUtils.rm configfile if File.exists? configfile
17
+ FileUtils.rm configfile if File.exist?(configfile)
18
18
  end
19
19
 
20
- describe ".load" do
21
- let(:config) { {'xx' => 'oo'} }
20
+ describe '.load' do
21
+ let(:config) { { 'xx' => 'oo' } }
22
22
 
23
- context "when there is no config file," do
24
- it "calls for setup" do
25
- subject.stub(:setup).with(configfile)
26
- File.open(configfile, "w") do |f|
27
- f.write YAML::dump(config)
23
+ context 'when there is no config file,' do
24
+ it 'calls for setup' do
25
+ Dir.stub(:exists?).and_return false
26
+ subject.stub(:setup).with(configfile, STDIN, STDOUT)
27
+ File.open(configfile, 'w') do |f|
28
+ f.write YAML.dump(config)
28
29
  end
29
30
  expect(subject.load options).to eq config
30
31
  end
@@ -32,12 +33,12 @@ describe Hackpad::Cli::Config do
32
33
 
33
34
  end
34
35
 
35
- describe ".setup" do
36
- context "when normal input is provided," do
36
+ describe '.setup' do
37
+ context 'when normal input is provided,' do
37
38
  let(:input) { StringIO.new }
38
39
  let(:output) { StringIO.new }
39
- it "handles setup interactively" do
40
- input.stub(:gets).and_return("client_id","secret","site")
40
+ it 'handles setup interactively' do
41
+ input.stub(:gets).and_return('client_id', 'secret', 'site')
41
42
  subject.send :setup, configfile, input, output
42
43
  expect(File.read configfile).to eq "---\nclient_id: client_id\nsecret: secret\nsite: site\n"
43
44
  end
@@ -1,41 +1,97 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'spec_helper'
4
- require "hackpad/cli"
5
- require "hackpad/cli/pad"
6
- require "hackpad/cli/api"
7
- require "hackpad/cli/store"
4
+ require 'hackpad/cli'
5
+ require 'hackpad/cli/pad'
6
+ require 'hackpad/cli/api'
7
+ require 'hackpad/cli/store'
8
8
 
9
9
  describe Hackpad::Cli::Pad do
10
10
 
11
- before :each do
12
- Hackpad::Cli::Api.stub(:read).with('123', 'txt').and_return("content\nand body")
13
- Hackpad::Cli::Api.stub(:read_options).with('123').and_return({"success" => "true", "options" => {}})
14
- options = {
15
- configdir: File.expand_path('../../../../files', __FILE__),
16
- workspace: 'default'
17
- }
18
- Hackpad::Cli::Store.prepare options
19
- @pad = Hackpad::Cli::Pad.new "123"
20
- @pad.load 'txt'
21
- end
11
+ let(:pad) { Hackpad::Cli::Pad.new '123' }
22
12
 
23
- after :each do
24
- FileUtils.rm_rf File.expand_path('../../../../files/default', __FILE__)
13
+ describe '.new' do
14
+ it { expect(pad.id).to eq '123' }
25
15
  end
26
16
 
27
- it "creates a new pad object" do
28
- expect(@pad.id).to eq "123"
17
+ describe '.cached?' do
18
+ before { Hackpad::Cli::Store.stub(:exists?).and_return true }
19
+ it { expect(pad.cached?).to be_true }
29
20
  end
30
21
 
31
- it "Can extract the title" do
32
- expect(@pad.title).to eq "content"
22
+ context 'when the pad has no data,' do
23
+ describe '.title' do
24
+ it { expect(pad.title).to eq nil }
25
+ end
26
+ describe '.chars' do
27
+ it { expect(pad.chars).to eq nil }
28
+ end
29
+ describe '.lines' do
30
+ it { expect(pad.lines).to eq nil }
31
+ end
33
32
  end
34
- it "Can count chars from content" do
35
- expect(@pad.chars).to be 16
33
+
34
+ context 'when the pad has only a content,' do
35
+ before { pad.instance_variable_set(:@content, "This\nis\nInformation!") }
36
+ describe '.title' do
37
+ it { expect(pad.title).to eq 'This' }
38
+ end
39
+ describe '.chars' do
40
+ it { expect(pad.chars).to eq 20 }
41
+ end
42
+ describe '.lines' do
43
+ it { expect(pad.lines).to eq 3 }
44
+ end
36
45
  end
37
- it "Can count lines from content" do
38
- expect(@pad.lines).to be 2
46
+
47
+ context 'when a pad is cached,' do
48
+ before { Hackpad::Cli::Store.stub(:exists?).and_return true }
49
+ context "when we don't want a refresh," do
50
+ describe '.load' do
51
+ context 'when unknown format is asked,' do
52
+ it { expect { pad.load 'xxx' }.to raise_error(Hackpad::Cli::UnknownFormat) }
53
+ end
54
+ context 'when pad has no id,' do
55
+ before { pad.send(:remove_instance_variable, :@id) }
56
+ it { expect { pad.load 'txt' }.to raise_error(Hackpad::Cli::UndefinedPad) }
57
+ end
58
+ context 'when all is ok,' do
59
+ before { pad.stub(:load_from_cache) }
60
+ it { expect { pad.load 'txt' }.not_to raise_error }
61
+ end
62
+ end
63
+ describe '.load_from_cache' do
64
+ let(:meta) { { 'options' => { 'guestPolicy' => 'open', 'isModerated' => false }, 'cached_at' => 'some time' } }
65
+ before { Hackpad::Cli::Store.stub(:read).with('123', 'txt').and_return("This\nis\nInformation!") }
66
+ before { Hackpad::Cli::Store.stub(:read_options).with('123').and_return(meta) }
67
+ before { pad.load_from_cache 'txt' }
68
+ it { expect(pad.content).to eq "This\nis\nInformation!" }
69
+ it { expect(pad.guest_policy).to eq 'open' }
70
+ it { expect(pad.moderated).to be_false }
71
+ it { expect(pad.cached_at).to eq 'some time' }
72
+ end
73
+ end
74
+ context 'when we want a refresh,' do
75
+ describe '.load' do
76
+ before { pad.stub(:load_from_api) }
77
+ it { expect { pad.load 'txt', true }.not_to raise_error }
78
+ end
79
+ describe '.load_from_api' do
80
+ let(:meta) { { 'options' => { 'guestPolicy' => 'open', 'isModerated' => false }, 'cached_at' => 'some time' } }
81
+ before { Hackpad::Cli::Api.stub(:read).with('123', 'txt').and_return("This\nis\nInformation!") }
82
+ before { Hackpad::Cli::Api.stub(:read_options).with('123').and_return(meta) }
83
+ context 'when we want to save to cache,' do
84
+ before { Hackpad::Cli::Store.stub(:save) }
85
+ before { Hackpad::Cli::Store.stub(:save_options) }
86
+ before { pad.load_from_api 'txt' }
87
+ it { expect(pad.content).to eq "This\nis\nInformation!" }
88
+ end
89
+ context "when we don't want to save to cache," do
90
+ before { pad.load_from_api 'txt', false }
91
+ it { expect(pad.content).to eq "This\nis\nInformation!" }
92
+ end
93
+ end
94
+ end
39
95
  end
40
96
 
41
97
  end
@@ -0,0 +1,70 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+ require 'hackpad/cli/padlist'
5
+
6
+ describe Hackpad::Cli::Padlist do
7
+
8
+ describe '.get_list' do
9
+ let(:output) { StringIO.new }
10
+
11
+ context 'when no refresh is asked and cache exists,' do
12
+ before { Hackpad::Cli::Store.stub(:exists?).and_return(true) }
13
+ before { Hackpad::Cli::Store.stub(:read_list).and_return(Array.new) }
14
+ it { expect(subject.get_list).to be_an(Array) }
15
+ end
16
+
17
+ context "when no refresh is asked and cache don't exist," do
18
+ let(:pad1) { OpenStruct.new(id: '123', title: 'title1') }
19
+ let(:pad2) { OpenStruct.new(id: '456', title: 'title2') }
20
+ before { Hackpad::Cli::Api.stub(:list).and_return(%w(123 456)) }
21
+ before { Hackpad::Cli::Store.stub(:exists?).and_return(false, false) }
22
+ before { Hackpad::Cli::Padlist.stub(:get_pad).and_return(pad1, pad2) }
23
+ before { Hackpad::Cli::Store.stub(:save_list) }
24
+ it { expect(subject.get_list(false, output)).to be_an(Array) }
25
+ it { expect(subject.get_list(false, output).count).to be 2 }
26
+ it { expect(subject.get_list(false, output)[1].title).to eq 'title2' }
27
+ end
28
+
29
+ end
30
+
31
+ describe '.get_pad' do
32
+ context 'when no refresh is asked,' do
33
+ let(:pad1) { OpenStruct.new(id: '123', title: 'title1') }
34
+ let(:pad) { double Hackpad::Cli::Pad }
35
+ before { Hackpad::Cli::Pad.stub(:new).and_return pad }
36
+ before { pad.stub(:load) }
37
+ before { pad.stub(:title).and_return('title1') }
38
+ it { expect(subject.get_pad('123')).to eq pad1 }
39
+ end
40
+ end
41
+
42
+ describe '.check_list' do
43
+ before { Hackpad::Cli::Api.stub(:list).and_return(%w(123 456)) }
44
+ let(:pad) { double Hackpad::Cli::Pad }
45
+ before { Hackpad::Cli::Pad.stub(:new).and_return(pad, pad) }
46
+
47
+ context 'when there is no new pad,' do
48
+ before { pad.stub(:cached?).and_return(true, true) }
49
+ it { expect(subject.check_list).to eq [] }
50
+ end
51
+
52
+ context 'when there is no one new pad,' do
53
+ let(:pad2) { OpenStruct.new(id: '456', title: 'title2') }
54
+ before { pad.stub(:cached?).and_return(true, false) }
55
+ before { pad.stub(:load) }
56
+ before { pad.stub(:title).and_return('title2') }
57
+ it { expect(subject.check_list).to eq [pad2] }
58
+ end
59
+
60
+ context 'when there is no 2 new pads,' do
61
+ let(:pad1) { OpenStruct.new(id: '123', title: 'title1') }
62
+ let(:pad2) { OpenStruct.new(id: '456', title: 'title2') }
63
+ before { pad.stub(:cached?).and_return(false, false) }
64
+ before { pad.stub(:load).and_return(nil, nil) }
65
+ before { pad.stub(:title).and_return('title1', 'title2') }
66
+ it { expect(subject.check_list).to eq [pad1, pad2] }
67
+ end
68
+ end
69
+
70
+ end
@@ -1,8 +1,8 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'spec_helper'
4
- require "hackpad/cli/runner"
5
- require "hackpad/cli/client"
4
+ require 'hackpad/cli/runner'
5
+ require 'hackpad/cli/client'
6
6
 
7
7
  describe Hackpad::Cli::Runner do
8
8
 
@@ -12,39 +12,67 @@ describe Hackpad::Cli::Runner do
12
12
  Hackpad::Cli::Client.stub(:new, {}).and_return(Object)
13
13
  end
14
14
 
15
- it "calls the search method in client class" do
15
+ it 'calls the stats method in client class' do
16
+ Object.stub(:stats)
17
+ cli.shell.mute do
18
+ cli.stats
19
+ end
20
+ end
21
+
22
+ it 'calls the search method in client class' do
16
23
  Object.stub(:search)
17
24
  cli.shell.mute do
18
- cli.search "xxx"
25
+ cli.search 'xxx'
19
26
  end
20
27
  end
21
28
 
22
- it "calls the list method in client class" do
29
+ it 'calls the list method in client class' do
23
30
  Object.stub(:list)
24
31
  cli.shell.mute do
25
32
  cli.list
26
33
  end
27
34
  end
28
35
 
29
- it "calls the list method in client class" do
36
+ it 'calls the list method in client class' do
30
37
  Object.stub(:list)
31
38
  cli.shell.mute do
32
39
  cli.list
33
40
  end
34
41
  end
35
42
 
36
- it "calls the info method in client class" do
43
+ it 'calls the check method in client class' do
44
+ Object.stub(:check)
45
+ cli.shell.mute do
46
+ cli.check
47
+ end
48
+ end
49
+
50
+ it 'calls the version method in client class' do
51
+ STDOUT.stub(:puts).with(Hackpad::Cli::VERSION)
52
+ cli.shell.mute do
53
+ cli.version
54
+ end
55
+ end
56
+
57
+ it 'calls the info method in client class' do
37
58
  Object.stub(:info)
38
59
  cli.shell.mute do
39
60
  cli.info 'pad'
40
61
  end
41
62
  end
42
63
 
43
- it "calls the show method in client class" do
64
+ it 'calls the show method in client class' do
44
65
  Object.stub(:show)
45
66
  cli.shell.mute do
46
67
  cli.show 'pad', 'md'
47
68
  end
48
69
  end
49
70
 
71
+ it 'calls the colors method in client class' do
72
+ String.stub(:color_matrix).with(' xoxo ')
73
+ cli.shell.mute do
74
+ cli.colors
75
+ end
76
+ end
77
+
50
78
  end
@@ -1,7 +1,8 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'spec_helper'
4
- require "hackpad/cli/store"
4
+ require 'hackpad/cli/store'
5
+ require 'hackpad/cli/pad'
5
6
 
6
7
  describe Hackpad::Cli::Store do
7
8
 
@@ -12,36 +13,36 @@ describe Hackpad::Cli::Store do
12
13
  subject.prepare options
13
14
  end
14
15
 
15
- describe ".read_list" do
16
+ describe '.read_list' do
16
17
  before { File.stub(:read).and_return("gy23ui first one\ngy3u4 second one\n23489g [some time] third") }
17
18
  let(:list) { subject.read_list }
18
19
  it { expect(list).to be_an Array }
19
20
  it { expect(list[0]).to be_an OpenStruct }
20
- it { expect(list[0].id).to eq "gy23ui" }
21
- it { expect(list[0].title).to eq "first one" }
22
- it { expect(list[2].id).to eq "23489g" }
23
- it { expect(list[2].title).to eq "third" }
24
- it { expect(list[2].cached_at).to eq "some time" }
21
+ it { expect(list[0].id).to eq 'gy23ui' }
22
+ it { expect(list[0].title).to eq 'first one' }
23
+ it { expect(list[2].id).to eq '23489g' }
24
+ it { expect(list[2].title).to eq 'third' }
25
+ it { expect(list[2].cached_at).to eq 'some time' }
25
26
  end
26
27
 
27
- describe ".exists?" do
28
+ describe '.exists?' do
28
29
 
29
- context "when refresh option is set," do
30
+ context 'when refresh option is set,' do
30
31
  let(:options) { { configdir: configdir, workspace: 'default', refresh: true } }
31
- before {
32
+ before do
32
33
  subject.prepare options
33
34
  FileUtils.touch File.join(configdir, 'default', 'pads', 'txt', 'xxx')
34
- }
35
+ end
35
36
  after { FileUtils.rm File.join(configdir, 'default', 'pads', 'txt', 'xxx') }
36
37
  it { expect(subject.exists? 'txt', 'xxx').to be false }
37
38
  end
38
39
 
39
- context "when refresh option is not set," do
40
+ context 'when refresh option is not set,' do
40
41
  context "when config file don't exist," do
41
42
  it { expect(subject.exists? 'txt', 'xxx').to be false }
42
43
  end
43
44
 
44
- context "when configfile exists," do
45
+ context 'when configfile exists,' do
45
46
  before { FileUtils.touch File.join(configdir, 'default', 'pads', 'txt', 'xxx') }
46
47
  after { FileUtils.rm File.join(configdir, 'default', 'pads', 'txt', 'xxx') }
47
48
  it { expect(subject.exists? 'txt', 'xxx').to be true }
@@ -50,4 +51,84 @@ describe Hackpad::Cli::Store do
50
51
 
51
52
  end
52
53
 
54
+ describe '.save' do
55
+ let(:padfile) { File.join(configdir, 'default', 'pads', 'txt', 'xxx') }
56
+ let(:content) { "This is content\n" }
57
+ before { subject.prepare options }
58
+ let(:pad) { double Hackpad::Cli::Pad }
59
+ before { pad.stub(:id).and_return 'xxx' }
60
+ before { pad.stub(:content).and_return content }
61
+ after { FileUtils.rm padfile }
62
+ it do
63
+ subject.save pad, 'txt'
64
+ expect(File.read(padfile)).to eq content
65
+ end
66
+ end
67
+
68
+ describe '.save_options' do
69
+ let(:padfile) { File.join(configdir, 'default', 'pads', 'meta', 'xxx') }
70
+ let(:content) { { thing: '123', other: 'option' } }
71
+ before { subject.prepare options }
72
+ after { FileUtils.rm padfile }
73
+ it do
74
+ subject.save_options 'xxx', content
75
+ expect(File.read(padfile)).to eq "{\n \"thing\": \"123\",\n \"other\": \"option\"\n}\n"
76
+ end
77
+ end
78
+
79
+ describe '.save_list' do
80
+ let(:padfile) { File.join(configdir, 'default', 'pads', 'padlist') }
81
+ let(:pads) { [OpenStruct.new(id: '123', cached_at: 'some time', title: 'title1')] }
82
+ before { subject.prepare options }
83
+ after { FileUtils.rm padfile }
84
+ it do
85
+ subject.save_list pads
86
+ expect(File.read(padfile)).to eq "123 [some time] title1\n"
87
+ end
88
+ end
89
+
90
+ describe '.read' do
91
+ let(:padfile) { File.join(configdir, 'default', 'pads', 'txt', 'xxx') }
92
+ let(:content) { "This is content\n" }
93
+ before { subject.prepare options }
94
+ let(:pad) { double Hackpad::Cli::Pad }
95
+ before { pad.stub(:id).and_return 'xxx' }
96
+ before { pad.stub(:content).and_return content }
97
+ before { subject.save pad, 'txt' }
98
+ after { FileUtils.rm padfile }
99
+
100
+ it { expect(subject.read 'xxx', 'txt').to eq content }
101
+ end
102
+
103
+ describe '.read_option' do
104
+ let(:padfile) { File.join(configdir, 'default', 'pads', 'meta', 'xxx') }
105
+ let(:content) { { 'thing' => '123', 'other' => 'option' } }
106
+ before { subject.prepare options }
107
+ before { subject.save_options 'xxx', content }
108
+ after { FileUtils.rm padfile }
109
+ it { expect(subject.read_options 'xxx').to eq content }
110
+ end
111
+
112
+ describe '.count_pads' do
113
+ let(:padfile) { File.join(configdir, 'default', 'pads', 'meta', 'xxx') }
114
+ let(:content) { { 'thing' => '123', 'other' => 'option' } }
115
+ before { subject.prepare options }
116
+ before { subject.save_options 'xxx', content }
117
+ after { FileUtils.rm padfile }
118
+ it { expect(subject.count_pads).to be 1 }
119
+ end
120
+
121
+ describe '.last_refresh' do
122
+ let(:timestamp) { Time.new(2012, 10, 31) }
123
+ let(:padlist) { File.join(configdir, 'default', 'pads', 'padlist') }
124
+ let(:pads) { [OpenStruct.new(id: '123', cached_at: 'some time', title: 'title1')] }
125
+ before { subject.prepare options }
126
+ before do
127
+ subject.save_list pads
128
+ FileUtils.touch padlist, mtime: timestamp
129
+ end
130
+ after { FileUtils.rm padlist }
131
+ it { expect(subject.last_refresh).to eq timestamp }
132
+ end
133
+
53
134
  end