Skip to content

Commit

Permalink
Add system specs
Browse files Browse the repository at this point in the history
  • Loading branch information
dachinat committed Nov 19, 2018
1 parent 425dab5 commit b963b72
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ PATH
bitbucket_pr (0.1.0)
commander (~> 4.4)
faraday (~> 0.15)
growl (~> 1.0)

GEM
remote: https://rubygems.org/
Expand All @@ -14,6 +15,7 @@ GEM
diff-lcs (1.3)
faraday (0.15.3)
multipart-post (>= 1.2, < 3)
growl (1.0.3)
highline (2.0.0)
jaro_winkler (1.5.1)
multipart-post (2.0.0)
Expand Down
1 change: 1 addition & 0 deletions bitbucket_pr.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ Gem::Specification.new do |spec|

spec.add_dependency "commander", "~> 4.4"
spec.add_dependency "faraday", "~> 0.15"
spec.add_dependency "growl", "~> 1.0"
end
7 changes: 7 additions & 0 deletions lib/bitbucket_pr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require "bitbucket_pr/version"
require "faraday"
require "json"
require "growl"

module BitbucketPr
def self.create(source, destination, options)
Expand Down Expand Up @@ -44,6 +45,12 @@ def self.create(source, destination, options)

say res.reason_phrase

if res.reason_phrase == "Created"
notify_ok "PR Created"
else
notify_error "PR not created"
end

begin
body = JSON.parse(res.body)
color("Failed: " + body["error"]["message"], :red) if (body["type"] == "error")
Expand Down
14 changes: 10 additions & 4 deletions lib/bitbucket_pr/system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@

module BitbucketPr
HOMES = ["HOME", "HOMEPATH"]
FILE_NAME = ".bitbucket_pr"

class << self
def home; ENV[HOMES.find { |h| ENV[h] != nil }]; end

def file; File.join(home, ".bitbucket_pr"); end
def file; File.join(home, FILE_NAME); end

def write(credentials)
credentials[:password] = [credentials[:password]].pack("u")
File.open(file, "w") { |file| file.write(credentials.to_yaml) }
begin
credentials[:password] = [credentials[:password]].pack("u")
File.open(file, "w") { |file| file.write(credentials.to_yaml) }
rescue => e
say("Internal error: " + e.message, :red)
false
end
end

def read
Expand All @@ -20,7 +26,7 @@ def read
credentials[:password] = credentials[:password].unpack("u")[0]
credentials
rescue => e
color("Internal error: " + e.message, :red)
say("Internal error: " + e.message, :red)
nil
end
end
Expand Down
4 changes: 0 additions & 4 deletions spec/bitbucket_pr_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@
it "has a version number" do
expect(BitbucketPr::VERSION).not_to be nil
end

it "does something useful" do
expect(false).to eq(true)
end
end
27 changes: 27 additions & 0 deletions spec/system_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
RSpec.describe BitbucketPr do
it "loads home path" do
expect(BitbucketPr.home).not_to be nil
end

it "resolves a file" do
expect(BitbucketPr.file).to include(BitbucketPr.home)
expect(BitbucketPr.file).to include(BitbucketPr::FILE_NAME)
end

it "writes a configuration file" do
testIO = StringIO.new
allow(File).to receive(:open).with(BitbucketPr.file,'w').and_yield( testIO )
BitbucketPr.write(username: "test1", password: "test2", repository: "test3")
expect(testIO.string.inspect).to include("test1")
expect(testIO.string.inspect).to include("%=&5S=#(`")
expect(testIO.string.inspect).to include("test3")
end

it "reads a configuration file" do
allow(File).to receive(:read).and_return("---\n:username: test1\n:password: \"%=&5S=#(`\\n\"\n:repository: test3\n")
out = BitbucketPr.read
expect(out[:username]).to eq("test1")
expect(out[:password]).to eq("test2")
expect(out[:repository]).to eq("test3")
end
end

0 comments on commit b963b72

Please sign in to comment.