Skip to content

Commit f402778

Browse files
committed
add specs for git-sourced libs
1 parent bcd35dc commit f402778

File tree

4 files changed

+93
-6
lines changed

4 files changed

+93
-6
lines changed

lib/bashly/commands/add.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ class Add < Base
1111
Specify a different libraries source. NAME can be:
1212
1313
* Path to a local libraries directory
14-
* Github repository, in the form of 'github:user/repo'
15-
* Remote git repository, in the form of 'git:clone_url.git'
14+
* GitHub (HTTPS) repository: github:user/repo[//path@ref]
15+
* GitHub (SSH) repository: github-ssh:user/repo[//path@ref]
16+
* Remote git repository: git:repo-url.git[//path@ref]
1617
USAGE
1718
option '-f --force', 'Overwrite existing files'
1819
option '-l --list', 'Show available libraries'

lib/bashly/library_source.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'fileutils'
2+
13
module Bashly
24
class LibrarySource
35
attr_reader :uri
@@ -46,7 +48,7 @@ def path
4648
def git_clone
4749
dir = Dir.mktmpdir 'bashly-libs-'
4850
safe_run "git clone --depth 1 #{git_specs[:url]} #{dir}"
49-
safe_run "git checkout #{git_specs[:ref]}" if git_specs[:ref]
51+
safe_run %[git -C "#{dir}" checkout #{git_specs[:ref]}] if git_specs[:ref]
5052

5153
"#{dir}#{git_specs[:path]}"
5254
end
@@ -67,7 +69,7 @@ def git_specs
6769
end
6870

6971
def safe_run(cmd)
70-
raise "Failed running command:\nm`#{cmd}`" unless system cmd
72+
raise "Failed running command:\nm`#{cmd}`" unless system "#{cmd} > /dev/null 2>&1"
7173
end
7274

7375
def transform_github_uri

spec/approvals/cli/add/help

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ Options:
1010
Specify a different libraries source. NAME can be:
1111

1212
* Path to a local libraries directory
13-
* Github repository, in the form of 'github:user/repo'
14-
* Remote git repository, in the form of 'git:clone_url.git'
13+
* GitHub (HTTPS) repository: github:user/repo[//path@ref]
14+
* GitHub (SSH) repository: github-ssh:user/repo[//path@ref]
15+
* Remote git repository: git:repo-url.git[//path@ref]
1516

1617
-f --force
1718
Overwrite existing files

spec/bashly/library_source_spec.rb

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,60 @@
11
require 'spec_helper'
22

33
describe LibrarySource do
4+
subject { described_class.new uri }
5+
6+
let(:uri) { nil }
7+
8+
after(:all) { described_class.new.cleanup }
9+
10+
describe '#uri' do
11+
context 'when it starts with github:' do
12+
let(:uri) { 'github:user/repo//the-path@the-ref' }
13+
14+
it 'is transformed to git over https' do
15+
expect(subject.uri).to eq 'git:https://github.com/user/repo.git//the-path@the-ref'
16+
end
17+
end
18+
19+
context 'when it starts with github-ssh:' do
20+
let(:uri) { 'github-ssh:user/repo//the-path@the-ref' }
21+
22+
it 'is transformed to git over ssh' do
23+
expect(subject.uri).to eq 'git:git@github.com:user/repo.git//the-path@the-ref'
24+
end
25+
end
26+
end
27+
28+
describe '#git?' do
29+
it 'returns false' do
30+
expect(subject).not_to be_git
31+
end
32+
33+
context 'when the uri starts with git:' do
34+
let(:uri) { 'git:/repo' }
35+
36+
it 'returns true' do
37+
expect(subject).to be_git
38+
end
39+
end
40+
41+
context 'when the uri starts with github:' do
42+
let(:uri) { 'github:/repo' }
43+
44+
it 'returns true' do
45+
expect(subject).to be_git
46+
end
47+
end
48+
49+
context 'when the uri starts with github-ssh:' do
50+
let(:uri) { 'github-ssh:/repo' }
51+
52+
it 'returns true' do
53+
expect(subject).to be_git
54+
end
55+
end
56+
end
57+
458
describe '#config' do
559
it 'returns the contents of the libraries.yml file' do
660
expect(subject.config).to be_a Hash
@@ -19,6 +73,28 @@
1973
end
2074
end
2175

76+
describe '#config_path' do
77+
it 'returns the path to libraries.yml' do
78+
expect(subject.config_path).to eq "#{subject.uri}/libraries.yml"
79+
end
80+
81+
context 'with a git source' do
82+
let(:uri) { 'github:dannyben/bashly//spec/fixtures/libraries@0058d77' }
83+
84+
it 'clones the repo to a temp directory and returns its path' do
85+
expect(subject.config_path).to match %r{/tmp/bashly-libs-.*/spec/fixtures/libraries/libraries.yml}
86+
end
87+
end
88+
89+
context 'with a directory that does not contain libraries.yml' do
90+
let(:uri) { 'spec' }
91+
92+
it 'raises an error' do
93+
expect { subject.config_path }.to raise_error('Cannot find spec/libraries.yml')
94+
end
95+
end
96+
end
97+
2298
describe '#libraries' do
2399
it 'returns a hash' do
24100
expect(subject.libraries).to be_a Hash
@@ -35,4 +111,11 @@
35111
expect(subject.libraries.values.map(&:class).uniq).to eq [Library]
36112
end
37113
end
114+
115+
describe '#cleanup' do
116+
it 'remoces all /tmp/bashly-libs-* directories' do
117+
expect(FileUtils).to receive(:rm_rf).with("#{Dir.tmpdir}/bashly-libs-*")
118+
subject.cleanup
119+
end
120+
end
38121
end

0 commit comments

Comments
 (0)