percy-client 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 38079cf3929991dac36dafcec0f7d74f9c028932
4
- data.tar.gz: 54075dce61dbd5973892706b30c10ae18176d057
3
+ metadata.gz: 07ab97ad5b889b060f38c6bfda3344133adc9c92
4
+ data.tar.gz: a18153dedd45120c932c0ceeefbce61d497d67ef
5
5
  SHA512:
6
- metadata.gz: 273681bc819851c01cc1a96aed84215737927d9e19862e430917311c09b8defd03ac4345800f72b4159ee6ee582898650c5111e84ecdbfb7dcf24a99bcc0f396
7
- data.tar.gz: 8e040316357f34c6014f1620aa5c69b36e8b0186b42523dbb9cc263b80f0e06520a8b19b55feb93e4ed230f0bbf5444182acaa7dc71159a395bc57940c70d1c2
6
+ metadata.gz: d451d0d9b8762beec56b2199a4b9d3e22bce3235d11b8915bb452cd206b2f400e3111bab917db7607ae2349ca4189b2d4f3b1a299ed74e071795e9331aa8048f
7
+ data.tar.gz: 512f86bfd854452cc5f9f8957cf30db13ec5a4c54a4b571ec99d98eff5fed3329f2b4d877fd13d97b26d343fd4ba9cb7064eee315cbf6c1d3f444c04efce2a29
@@ -1,7 +1,7 @@
1
1
  require 'json'
2
2
  require 'percy/config'
3
+ require 'percy/client/environment'
3
4
  require 'percy/client/connection'
4
- require 'percy/client/local_git'
5
5
  require 'percy/client/version'
6
6
  require 'percy/client/builds'
7
7
  require 'percy/client/snapshots'
@@ -2,7 +2,9 @@ module Percy
2
2
  class Client
3
3
  module Builds
4
4
  def create_build(repo, options = {})
5
- commit_data = options[:commit_data] || Percy::Client::LocalGit.commit
5
+ pull_request_number = options[:pull_request_number] ||
6
+ Percy::Client::Environment.pull_request_number
7
+ commit_data = options[:commit_data] || Percy::Client::Environment.commit
6
8
  data = {
7
9
  'data' => {
8
10
  'type' => 'builds',
@@ -15,7 +17,7 @@ module Percy
15
17
  'commit-committer-name' => commit_data[:committer_name],
16
18
  'commit-committer-email' => commit_data[:committer_email],
17
19
  'commit-message' => commit_data[:message],
18
- 'pull-request-number' => nil,
20
+ 'pull-request-number' => pull_request_number,
19
21
  },
20
22
  }
21
23
  }
@@ -0,0 +1,111 @@
1
+ module Percy
2
+ class Client
3
+ module Environment
4
+ GIT_FORMAT_LINES = [
5
+ 'COMMIT_SHA:%H',
6
+ 'AUTHOR_NAME:%an',
7
+ 'AUTHOR_EMAIL:%ae',
8
+ 'COMMITTER_NAME:%an',
9
+ 'COMMITTER_EMAIL:%ae',
10
+ 'COMMITTED_DATE:%ai',
11
+ # Note: order is important, this must come last because the regex is a multiline match.
12
+ 'COMMIT_MESSAGE:%B'
13
+ ].freeze
14
+
15
+ class Error < Exception; end
16
+ class RepoNotFoundError < Exception; end
17
+ class BranchNotFoundError < Exception; end
18
+
19
+ def self.current_ci
20
+ return :travis if ENV['TRAVIS_BUILD_ID']
21
+ return :jenkins if ENV['JENKINS_URL'] && ENV['ghprbPullId'] # Pull Request Builder plugin.
22
+ return :circle if ENV['CIRCLECI']
23
+ end
24
+
25
+ def self.commit_sha
26
+ return ENV['PERCY_COMMIT'] if ENV['PERCY_COMMIT']
27
+
28
+ case current_ci
29
+ when :jenkins
30
+ ENV['ghprbActualCommit']
31
+ when :travis
32
+ ENV['TRAVIS_COMMIT']
33
+ when :circle
34
+ ENV['CIRCLE_SHA1']
35
+ else
36
+ 'HEAD'
37
+ end
38
+ end
39
+
40
+ def self.commit
41
+ format = GIT_FORMAT_LINES.join('%n') # "git show" format uses %n for newlines.
42
+ output = `git show --quiet #{commit_sha} --format="#{format}"`.strip
43
+ data = {
44
+ sha: output.match(/COMMIT_SHA:(.*)/)[1],
45
+ branch: branch,
46
+ committed_at: output.match(/COMMITTED_DATE:(.*)/)[1],
47
+ author_name: output.match(/AUTHOR_NAME:(.*)/)[1],
48
+ author_email: output.match(/AUTHOR_EMAIL:(.*)/)[1],
49
+ committer_name: output.match(/COMMITTER_NAME:(.*)/)[1],
50
+ committer_email: output.match(/COMMITTER_EMAIL:(.*)/)[1],
51
+ message: output.match(/COMMIT_MESSAGE:(.*)/m)[1],
52
+ }
53
+ end
54
+
55
+ # The name of the target branch that the build will be compared against.
56
+ def self.branch
57
+ return ENV['PERCY_BRANCH'] if ENV['PERCY_BRANCH']
58
+
59
+ result = case current_ci
60
+ when :jenkins
61
+ ENV['ghprbTargetBranch']
62
+ when :travis
63
+ ENV['TRAVIS_BRANCH']
64
+ when :circle
65
+ ENV['CIRCLE_BRANCH']
66
+ else
67
+ # Discover from current git repo branch name.
68
+ `git rev-parse --abbrev-ref HEAD`.strip
69
+ end
70
+ if result == ''
71
+ raise Percy::Client::Environment::BranchNotFoundError.new('No target branch found.')
72
+ end
73
+ result
74
+ end
75
+
76
+ def self.repo
77
+ return ENV['PERCY_REPO_SLUG'] if ENV['PERCY_REPO_SLUG']
78
+
79
+ case current_ci
80
+ when :travis
81
+ ENV['TRAVIS_REPO_SLUG']
82
+ when :circle
83
+ "#{ENV['CIRCLE_PROJECT_USERNAME']}/#{ENV['CIRCLE_PROJECT_REPONAME']}"
84
+ else
85
+ origin_url = `git config --get remote.origin.url`
86
+ if origin_url == ''
87
+ raise Percy::Client::Environment::RepoNotFoundError.new('No local git repository found.')
88
+ end
89
+ match = origin_url.match(Regexp.new('[:/]([^/]+\/[^/]+)\.git'))
90
+ match[1]
91
+ end
92
+ end
93
+
94
+ def self.pull_request_number
95
+ return ENV['PERCY_PULL_REQUEST'] if ENV['PERCY_PULL_REQUEST']
96
+
97
+ case current_ci
98
+ when :jenkins
99
+ # GitHub Pull Request Builder plugin.
100
+ ENV['ghprbPullId']
101
+ when :travis
102
+ ENV['TRAVIS_PULL_REQUEST'] if ENV['TRAVIS_PULL_REQUEST'] != 'false'
103
+ when :circle
104
+ if ENV['CI_PULL_REQUESTS'] && ENV['CI_PULL_REQUESTS'] != ''
105
+ ENV['CI_PULL_REQUESTS'].split(',')[0]
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
@@ -12,9 +12,15 @@ module Percy
12
12
  attr_accessor :mimetype
13
13
  attr_accessor :content
14
14
 
15
- def initialize(sha, resource_url, options = {})
16
- @sha = sha
15
+ def initialize(resource_url, options = {})
17
16
  @resource_url = resource_url
17
+
18
+ if !options[:sha] && !options[:content]
19
+ raise ArgumentError, 'Either "sha" or "content" must be given.'
20
+ end
21
+ @sha = options[:sha] || Digest::SHA256.hexdigest(options[:content])
22
+ @content = options[:content]
23
+
18
24
  @is_root = options[:is_root]
19
25
  @mimetype = options[:mimetype]
20
26
 
@@ -1,5 +1,5 @@
1
1
  module Percy
2
2
  class Client
3
- VERSION = '0.1.4'
3
+ VERSION = '0.1.5'
4
4
  end
5
5
  end
@@ -28,7 +28,7 @@ module Percy
28
28
  end
29
29
 
30
30
  def repo
31
- @repo ||= ENV['PERCY_REPO'] || Percy::Client::LocalGit.repo
31
+ @repo ||= ENV['PERCY_REPO'] || Percy::Client::Environment.repo
32
32
  end
33
33
  end
34
34
  end
@@ -7,7 +7,7 @@ http_interactions:
7
7
  encoding: UTF-8
8
8
  string: '{"data":{"type":"builds","attributes":{"commit-sha":"85e1b2fc5e80c462bb281d4a9b72b3d86e9f5e8f","commit-branch":"master","commit-committed-at":"2015-05-16
9
9
  19:39:41 -0700","commit-author-name":"<COMMIT_AUTHOR_NAME>","commit-author-email":"<COMMIT_AUTHOR_EMAIL>","commit-committer-name":"<COMMIT_AUTHOR_NAME>","commit-committer-email":"<COMMIT_AUTHOR_EMAIL>","commit-message":"Checkpoint
10
- some initial resources and snapshots work.","pull-request-number":null}}}'
10
+ some initial resources and snapshots work.","pull-request-number":"123"}}}'
11
11
  headers:
12
12
  User-Agent:
13
13
  - Faraday v0.9.1
@@ -56,9 +56,9 @@ http_interactions:
56
56
  - Keep-Alive
57
57
  body:
58
58
  encoding: UTF-8
59
- string: '{"data":{"id":"14","type":"builds","attributes":{"state":"pending","is-pull-request":false,"pull-request-number":0,"pull-request-title":null,"approved-at":null,"created-at":"2015-05-17T04:14:20.322Z","updated-at":"2015-05-17T04:14:20.322Z"},"links":{"self":"/api/v1/builds/14","commit":{"self":"/api/v1/builds/14/links/commit","related":"/api/v1/builds/14/commit","linkage":{"type":"commits","id":"1"}},"repo":{"self":"/api/v1/builds/14/links/repo","related":"/api/v1/builds/14/repo"},"base-build":{"self":"/api/v1/builds/14/links/base-build","related":"/api/v1/builds/14/base-build","linkage":{"type":"builds","id":"13"}},"approved-by":{"self":"/api/v1/builds/14/links/approved-by","related":"/api/v1/builds/14/approved-by"},"snapshots":{"self":"/api/v1/builds/14/links/snapshots","related":"/api/v1/builds/14/snapshots"},"comparisons":{"self":"/api/v1/builds/14/links/comparisons","related":"/api/v1/builds/14/comparisons"}},"meta":{"finalize-link":"/api/v1/builds/14/finalize","approve-link":"/api/v1/builds/14/approve"}},"included":[{"id":"1","type":"commits","attributes":{"sha":"85e1b2fc5e80c462bb281d4a9b72b3d86e9f5e8f","branch":"master","message":"Checkpoint
59
+ string: '{"data":{"id":"14","type":"builds","attributes":{"state":"pending","is-pull-request":true,"pull-request-number":123,"pull-request-title":null,"approved-at":null,"created-at":"2015-05-17T04:14:20.322Z","updated-at":"2015-05-17T04:14:20.322Z"},"links":{"self":"/api/v1/builds/14","commit":{"self":"/api/v1/builds/14/links/commit","related":"/api/v1/builds/14/commit","linkage":{"type":"commits","id":"1"}},"repo":{"self":"/api/v1/builds/14/links/repo","related":"/api/v1/builds/14/repo"},"base-build":{"self":"/api/v1/builds/14/links/base-build","related":"/api/v1/builds/14/base-build","linkage":{"type":"builds","id":"13"}},"approved-by":{"self":"/api/v1/builds/14/links/approved-by","related":"/api/v1/builds/14/approved-by"},"snapshots":{"self":"/api/v1/builds/14/links/snapshots","related":"/api/v1/builds/14/snapshots"},"comparisons":{"self":"/api/v1/builds/14/links/comparisons","related":"/api/v1/builds/14/comparisons"}},"meta":{"finalize-link":"/api/v1/builds/14/finalize","approve-link":"/api/v1/builds/14/approve"}},"included":[{"id":"1","type":"commits","attributes":{"sha":"85e1b2fc5e80c462bb281d4a9b72b3d86e9f5e8f","branch":"master","message":"Checkpoint
60
60
  some initial resources and snapshots work.","committed-at":"2015-05-16 19:39:41
61
61
  -0700","author-name":"<COMMIT_AUTHOR_NAME>","committer-name":"<COMMIT_AUTHOR_NAME>","created-at":"2015-05-17T04:02:54.000Z","updated-at":"2015-05-17T04:02:54.000Z"},"links":{"self":"/api/v1/commits/1"}},{"id":"13","type":"builds","attributes":{"state":"finished","is-pull-request":false,"pull-request-number":0,"pull-request-title":null,"approved-at":null,"created-at":"2015-05-17T04:14:20.000Z","updated-at":"2015-05-17T04:14:20.000Z"},"links":{"self":"/api/v1/builds/13","commit":{"self":"/api/v1/builds/13/links/commit","related":"/api/v1/builds/13/commit","linkage":{"type":"commits","id":"1"}},"repo":{"self":"/api/v1/builds/13/links/repo","related":"/api/v1/builds/13/repo"},"base-build":{"self":"/api/v1/builds/13/links/base-build","related":"/api/v1/builds/13/base-build"},"approved-by":{"self":"/api/v1/builds/13/links/approved-by","related":"/api/v1/builds/13/approved-by"},"snapshots":{"self":"/api/v1/builds/13/links/snapshots","related":"/api/v1/builds/13/snapshots"},"comparisons":{"self":"/api/v1/builds/13/links/comparisons","related":"/api/v1/builds/13/comparisons"}},"meta":{"finalize-link":"/api/v1/builds/13/finalize","approve-link":"/api/v1/builds/13/approve"}}]}'
62
- http_version:
62
+ http_version:
63
63
  recorded_at: Sun, 17 May 2015 04:14:20 GMT
64
64
  recorded_with: VCR 2.9.3
@@ -1,5 +1,7 @@
1
1
  RSpec.describe Percy::Client::Builds, :vcr do
2
2
  describe '#create_build' do
3
+ before(:each) { ENV['PERCY_PULL_REQUEST'] = '123' }
4
+ after(:each) { ENV['PERCY_PULL_REQUEST'] = nil }
3
5
  it 'creates a build' do
4
6
  build = Percy.create_build('fotinakis/percy-examples')
5
7
  expect(build).to be
@@ -7,6 +9,8 @@ RSpec.describe Percy::Client::Builds, :vcr do
7
9
  expect(build['data']['id']).to be
8
10
  expect(build['data']['type']).to eq('builds')
9
11
  expect(build['data']['attributes']['state']).to eq('pending')
12
+ expect(build['data']['attributes']['is-pull-request']).to be_truthy
13
+ expect(build['data']['attributes']['pull-request-number']).to eq(123)
10
14
  end
11
15
  end
12
16
  describe '#finalize_build' do
@@ -0,0 +1,217 @@
1
+ RSpec.describe Percy::Client::Environment do
2
+ def clear_env_vars
3
+ # Unset Percy vars.
4
+ ENV['PERCY_COMMIT'] = nil
5
+ ENV['PERCY_BRANCH'] = nil
6
+ ENV['PERCY_PULL_REQUEST'] = nil
7
+ ENV['PERCY_REPO_SLUG'] = nil
8
+
9
+ # Unset Travis vars.
10
+ ENV['TRAVIS_BUILD_ID'] = nil
11
+ ENV['TRAVIS_COMMIT'] = nil
12
+ ENV['TRAVIS_BRANCH'] = nil
13
+ ENV['TRAVIS_PULL_REQUEST'] = nil
14
+ ENV['TRAVIS_REPO_SLUG'] = nil
15
+
16
+ # Unset Jenkins vars.
17
+ ENV['JENKINS_URL'] = nil
18
+ ENV['ghprbPullId'] = nil
19
+ ENV['ghprbActualCommit'] = nil
20
+ ENV['ghprbTargetBranch'] = nil
21
+
22
+ # Unset Circle CI vars.
23
+ ENV['CIRCLECI'] = nil
24
+ ENV['CIRCLE_SHA1'] = nil
25
+ ENV['CIRCLE_BRANCH'] = nil
26
+ ENV['CIRCLE_PROJECT_USERNAME'] = nil
27
+ ENV['CIRCLE_PROJECT_REPONAME'] = nil
28
+ ENV['CI_PULL_REQUESTS'] = nil
29
+ end
30
+
31
+ before(:each) do
32
+ @original_env = {
33
+ 'TRAVIS_BUILD_ID' => ENV['TRAVIS_BUILD_ID'],
34
+ 'TRAVIS_COMMIT' => ENV['TRAVIS_COMMIT'],
35
+ 'TRAVIS_BRANCH' => ENV['TRAVIS_BRANCH'],
36
+ 'TRAVIS_PULL_REQUEST' => ENV['TRAVIS_PULL_REQUEST'],
37
+ 'TRAVIS_REPO_SLUG' => ENV['TRAVIS_REPO_SLUG'],
38
+ }
39
+ clear_env_vars
40
+ end
41
+ after(:each) do
42
+ clear_env_vars
43
+ ENV['TRAVIS_BUILD_ID'] = @original_env['TRAVIS_BUILD_ID']
44
+ ENV['TRAVIS_COMMIT'] = @original_env['TRAVIS_COMMIT']
45
+ ENV['TRAVIS_BRANCH'] = @original_env['TRAVIS_BRANCH']
46
+ ENV['TRAVIS_PULL_REQUEST'] = @original_env['TRAVIS_PULL_REQUEST']
47
+ ENV['TRAVIS_REPO_SLUG'] = @original_env['TRAVIS_REPO_SLUG']
48
+ end
49
+
50
+ context 'no known CI environment' do
51
+ describe '#current_ci' do
52
+ it 'is nil' do
53
+ expect(Percy::Client::Environment.current_ci).to be_nil
54
+ end
55
+ end
56
+ describe '#branch' do
57
+ it 'reads from the current local repo' do
58
+ expect(Percy::Client::Environment.branch).to_not be_empty
59
+ end
60
+ it 'can be overridden with PERCY_BRANCH' do
61
+ ENV['PERCY_BRANCH'] = 'test-branch'
62
+ expect(Percy::Client::Environment.branch).to eq('test-branch')
63
+ end
64
+ end
65
+ describe '#commit_sha' do
66
+ it 'reads from the current local repo' do
67
+ expect(Percy::Client::Environment.commit_sha).to eq('HEAD')
68
+ end
69
+ it 'can be overridden with PERCY_COMMIT' do
70
+ ENV['PERCY_COMMIT'] = 'test-commit'
71
+ expect(Percy::Client::Environment.commit_sha).to eq('test-commit')
72
+ end
73
+ end
74
+ describe '#pull_request_number' do
75
+ it 'returns nil if no CI environment' do
76
+ expect(Percy::Client::Environment.pull_request_number).to be_nil
77
+ end
78
+ it 'can be overridden with PERCY_PULL_REQUEST' do
79
+ ENV['PERCY_PULL_REQUEST'] = '123'
80
+ ENV['TRAVIS_BUILD_ID'] = '1234'
81
+ ENV['TRAVIS_PULL_REQUEST'] = '256'
82
+ expect(Percy::Client::Environment.pull_request_number).to eq('123')
83
+ end
84
+ end
85
+ describe '#repo' do
86
+ it 'returns the current local repo name' do
87
+ expect(Percy::Client::Environment.repo).to eq('percy/percy-client')
88
+ end
89
+ it 'can be overridden with PERCY_REPO_SLUG' do
90
+ ENV['PERCY_REPO_SLUG'] = 'percy/slug'
91
+ expect(Percy::Client::Environment.repo).to eq('percy/slug')
92
+ end
93
+ end
94
+ end
95
+ context 'in Jenkins CI' do
96
+ before(:each) do
97
+ ENV['JENKINS_URL'] = 'http://localhost:8080/'
98
+ ENV['ghprbPullId'] = '123'
99
+ ENV['ghprbTargetBranch'] = 'jenkins-target-branch'
100
+ ENV['ghprbActualCommit'] = 'jenkins-actual-commit'
101
+ end
102
+
103
+ describe '#current_ci' do
104
+ it 'is :jenkins' do
105
+ expect(Percy::Client::Environment.current_ci).to eq(:jenkins)
106
+ end
107
+ end
108
+ describe '#branch' do
109
+ it 'reads from the CI environment' do
110
+ expect(Percy::Client::Environment.branch).to eq('jenkins-target-branch')
111
+ end
112
+ end
113
+ describe '#commit_sha' do
114
+ it 'reads from the CI environment' do
115
+ expect(Percy::Client::Environment.commit_sha).to eq('jenkins-actual-commit')
116
+ end
117
+ end
118
+ describe '#pull_request_number' do
119
+ it 'reads from the CI environment' do
120
+ expect(Percy::Client::Environment.pull_request_number).to eq('123')
121
+ end
122
+ end
123
+ describe '#repo' do
124
+ it 'returns the current local repo name' do
125
+ expect(Percy::Client::Environment.repo).to eq('percy/percy-client')
126
+ end
127
+ end
128
+ end
129
+ context 'in Travis CI' do
130
+ before(:each) do
131
+ ENV['TRAVIS_BUILD_ID'] = '1234'
132
+ ENV['TRAVIS_PULL_REQUEST'] = '256'
133
+ ENV['TRAVIS_REPO_SLUG'] = 'travis/repo-slug'
134
+ ENV['TRAVIS_COMMIT'] = 'travis-commit-sha'
135
+ ENV['TRAVIS_BRANCH'] = 'travis-branch'
136
+ end
137
+
138
+ describe '#current_ci' do
139
+ it 'is :travis' do
140
+ expect(Percy::Client::Environment.current_ci).to eq(:travis)
141
+ end
142
+ end
143
+ describe '#branch' do
144
+ it 'reads from the CI environment' do
145
+ expect(Percy::Client::Environment.branch).to eq('travis-branch')
146
+ end
147
+ end
148
+ describe '#commit_sha' do
149
+ it 'reads from the CI environment' do
150
+ expect(Percy::Client::Environment.commit_sha).to eq('travis-commit-sha')
151
+ end
152
+ end
153
+ describe '#pull_request_number' do
154
+ it 'reads from the CI environment' do
155
+ expect(Percy::Client::Environment.pull_request_number).to eq('256')
156
+ end
157
+ end
158
+ describe '#repo' do
159
+ it 'reads from the CI environment' do
160
+ expect(Percy::Client::Environment.repo).to eq('travis/repo-slug')
161
+ end
162
+ end
163
+ end
164
+ context 'in Circle CI' do
165
+ before(:each) do
166
+ ENV['CIRCLECI'] = 'true'
167
+ ENV['CIRCLE_BRANCH'] = 'circle-branch'
168
+ ENV['CIRCLE_SHA1'] = 'circle-commit-sha'
169
+ ENV['CIRCLE_PROJECT_USERNAME'] = 'circle'
170
+ ENV['CIRCLE_PROJECT_REPONAME'] = 'repo-name'
171
+ ENV['CI_PULL_REQUESTS'] = '123,234'
172
+ end
173
+
174
+ describe '#current_ci' do
175
+ it 'is :circle' do
176
+ expect(Percy::Client::Environment.current_ci).to eq(:circle)
177
+ end
178
+ end
179
+ describe '#branch' do
180
+ it 'reads from the CI environment' do
181
+ expect(Percy::Client::Environment.branch).to eq('circle-branch')
182
+ end
183
+ end
184
+ describe '#commit_sha' do
185
+ it 'reads from the CI environment' do
186
+ expect(Percy::Client::Environment.commit_sha).to eq('circle-commit-sha')
187
+ end
188
+ end
189
+
190
+ describe '#pull_request_number' do
191
+ it 'reads from the CI environment' do
192
+ expect(Percy::Client::Environment.pull_request_number).to eq('123')
193
+ end
194
+ end
195
+ describe '#repo' do
196
+ it 'reads from the CI environment' do
197
+ expect(Percy::Client::Environment.repo).to eq('circle/repo-name')
198
+ end
199
+ end
200
+ end
201
+ describe 'local git repo methods' do
202
+ describe '#commit' do
203
+ it 'returns current local commit data' do
204
+ commit = Percy::Client::Environment.commit
205
+ expect(commit[:author_email]).to match(/.+@.+\..+/)
206
+ expect(commit[:author_name]).to_not be_empty
207
+ expect(commit[:branch]).to_not be_empty
208
+ expect(commit[:committed_at]).to_not be_empty
209
+ expect(commit[:committer_email]).to_not be_empty
210
+ expect(commit[:committer_name]).to_not be_empty
211
+ expect(commit[:message]).to_not be_empty
212
+ expect(commit[:sha]).to_not be_empty
213
+ expect(commit[:sha].length).to eq(40)
214
+ end
215
+ end
216
+ end
217
+ end
@@ -6,7 +6,7 @@ RSpec.describe Percy::Client::Resources, :vcr do
6
6
 
7
7
  describe 'Percy::Client::Resource' do
8
8
  it 'can be initialized with minimal data' do
9
- resource = Percy::Client::Resource.new(sha, '/foo.html')
9
+ resource = Percy::Client::Resource.new('/foo.html', sha: sha)
10
10
  expect(resource.serialize).to eq({
11
11
  'type' => 'resources',
12
12
  'id' => sha,
@@ -16,7 +16,13 @@ RSpec.describe Percy::Client::Resources, :vcr do
16
16
  })
17
17
  end
18
18
  it 'can be initialized with all data' do
19
- resource = Percy::Client::Resource.new(sha, '/foo.html', is_root: true, mimetype: 'text/html')
19
+ resource = Percy::Client::Resource.new(
20
+ '/foo.html',
21
+ sha: sha,
22
+ is_root: true,
23
+ mimetype: 'text/html',
24
+ content: content,
25
+ )
20
26
  expect(resource.serialize).to eq({
21
27
  'type' => 'resources',
22
28
  'id' => sha,
@@ -25,11 +31,14 @@ RSpec.describe Percy::Client::Resources, :vcr do
25
31
  'is-root' => true,
26
32
  })
27
33
  end
34
+ it 'errors if not given sha or content' do
35
+ expect { Percy::Client::Resource.new('/foo.html') }.to raise_error(ArgumentError)
36
+ end
28
37
  end
29
38
  describe '#upload_resource' do
30
39
  it 'returns true with success' do
31
40
  build = Percy.create_build('fotinakis/percy-examples')
32
- resources = [Percy::Client::Resource.new(sha, '/foo/test.html', is_root: true)]
41
+ resources = [Percy::Client::Resource.new('/foo/test.html', sha: sha, is_root: true)]
33
42
  Percy.create_snapshot(build['data']['id'], resources, name: 'homepage')
34
43
 
35
44
  # Verify that upload_resource hides conflict errors, though they are output to stderr.
@@ -6,8 +6,8 @@ RSpec.describe Percy::Client::Snapshots, :vcr do
6
6
  it 'creates a build' do
7
7
  build = Percy.create_build('fotinakis/percy-examples')
8
8
  resources = []
9
- resources << Percy::Client::Resource.new(sha, '/foo/test.html', is_root: true)
10
- resources << Percy::Client::Resource.new(sha, '/css/test.css')
9
+ resources << Percy::Client::Resource.new('/foo/test.html', sha: sha, is_root: true)
10
+ resources << Percy::Client::Resource.new('/css/test.css', sha: sha)
11
11
  snapshot = Percy.create_snapshot(build['data']['id'], resources, name: 'homepage')
12
12
 
13
13
  expect(snapshot['data']).to be
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: percy-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Perceptual Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-21 00:00:00.000000000 Z
11
+ date: 2015-05-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -127,7 +127,7 @@ files:
127
127
  - lib/percy/client.rb
128
128
  - lib/percy/client/builds.rb
129
129
  - lib/percy/client/connection.rb
130
- - lib/percy/client/local_git.rb
130
+ - lib/percy/client/environment.rb
131
131
  - lib/percy/client/resources.rb
132
132
  - lib/percy/client/snapshots.rb
133
133
  - lib/percy/client/version.rb
@@ -140,7 +140,7 @@ files:
140
140
  - spec/cassettes/Percy_Client_Snapshots/_create_snapshot/fails_if_no_resources_are_given.yml
141
141
  - spec/lib/percy/client/builds_spec.rb
142
142
  - spec/lib/percy/client/connection_spec.rb
143
- - spec/lib/percy/client/local_git_spec.rb
143
+ - spec/lib/percy/client/environment_spec.rb
144
144
  - spec/lib/percy/client/resources_spec.rb
145
145
  - spec/lib/percy/client/snapshots_spec.rb
146
146
  - spec/lib/percy/client_spec.rb
@@ -179,7 +179,7 @@ test_files:
179
179
  - spec/cassettes/Percy_Client_Snapshots/_create_snapshot/fails_if_no_resources_are_given.yml
180
180
  - spec/lib/percy/client/builds_spec.rb
181
181
  - spec/lib/percy/client/connection_spec.rb
182
- - spec/lib/percy/client/local_git_spec.rb
182
+ - spec/lib/percy/client/environment_spec.rb
183
183
  - spec/lib/percy/client/resources_spec.rb
184
184
  - spec/lib/percy/client/snapshots_spec.rb
185
185
  - spec/lib/percy/client_spec.rb
@@ -1,49 +0,0 @@
1
- module Percy
2
- class Client
3
- module LocalGit
4
- GIT_FORMAT_LINES = [
5
- 'COMMIT_SHA:%H',
6
- 'AUTHOR_NAME:%an',
7
- 'AUTHOR_EMAIL:%ae',
8
- 'COMMITTER_NAME:%an',
9
- 'COMMITTER_EMAIL:%ae',
10
- 'COMMITTED_DATE:%ai',
11
- # Note: order is important, this must come last because the regex is a multiline match.
12
- 'COMMIT_MESSAGE:%B'
13
- ].freeze
14
-
15
- class Error < Exception; end
16
- class NoLocalRepo < Exception; end
17
-
18
- def self.commit
19
- commit = ENV['PERCY_COMMIT'] || 'HEAD'
20
- branch = ENV['PERCY_BRANCH'] || `git rev-parse --abbrev-ref HEAD`.strip
21
- if branch == ''
22
- raise Percy::Client::LocalGit::NoLocalRepo.new('No local git repository found.')
23
- end
24
-
25
- format = GIT_FORMAT_LINES.join('%n') # "git show" format uses %n for newlines.
26
- output = `git show --quiet #{commit} --format="#{format}"`.strip
27
- data = {
28
- sha: output.match(/COMMIT_SHA:(.*)/)[1],
29
- branch: branch,
30
- committed_at: output.match(/COMMITTED_DATE:(.*)/)[1],
31
- author_name: output.match(/AUTHOR_NAME:(.*)/)[1],
32
- author_email: output.match(/AUTHOR_EMAIL:(.*)/)[1],
33
- committer_name: output.match(/COMMITTER_NAME:(.*)/)[1],
34
- committer_email: output.match(/COMMITTER_EMAIL:(.*)/)[1],
35
- message: output.match(/COMMIT_MESSAGE:(.*)/m)[1],
36
- }
37
- end
38
-
39
- def self.repo
40
- origin_url = `git config --get remote.origin.url`
41
- if origin_url == ''
42
- raise Percy::Client::LocalGit::NoLocalRepo.new('No local git repository found.')
43
- end
44
- match = origin_url.match(Regexp.new('[:/]([^/]+\/[^/]+)\.git'))
45
- match[1]
46
- end
47
- end
48
- end
49
- end
@@ -1,21 +0,0 @@
1
- RSpec.describe Percy::Client::LocalGit do
2
- describe '#repo' do
3
- it 'returns the current local repo name' do
4
- expect(Percy::Client::LocalGit.repo).to eq('percy/percy-client')
5
- end
6
- end
7
- describe '#commit' do
8
- it 'returns current local commit data' do
9
- commit = Percy::Client::LocalGit.commit
10
- expect(commit[:author_email]).to match(/.+@.+\..+/)
11
- expect(commit[:author_name]).to_not be_empty
12
- expect(commit[:branch]).to_not be_empty
13
- expect(commit[:committed_at]).to_not be_empty
14
- expect(commit[:committer_email]).to_not be_empty
15
- expect(commit[:committer_name]).to_not be_empty
16
- expect(commit[:message]).to_not be_empty
17
- expect(commit[:sha]).to_not be_empty
18
- expect(commit[:sha].length).to eq(40)
19
- end
20
- end
21
- end