def build_resources(self): resources = [] if not self.root_dir: return resources for root, dirs, files in os.walk(self.root_dir, followlinks=True): for file_name in files: path = os.path.join(root, file_name) if os.path.getsize(path) > MAX_FILESIZE_BYTES: continue with open(path, 'rb') as f: content = f.read() path_for_url = pathname2url( path.replace(self.root_dir, '', 1)) if self.base_url[-1] == '/' and path_for_url[0] == '/': path_for_url = path_for_url.replace('/', '', 1) resource_url = "{0}{1}".format(self.base_url, path_for_url) resource = percy.Resource( resource_url=resource_url, sha=utils.sha256hash(content), local_path=os.path.abspath(path), ) resources.append(resource) return resources
def test_create_build(self, mock): fixture_path = os.path.join(FIXTURES_DIR, 'build_response.json') build_fixture = open(fixture_path).read() mock.post('https://percy.io/api/v1/repos/foo/bar/builds/', text=build_fixture) resources = [ percy.Resource(resource_url='/main.css', is_root=False, content='foo'), ] build_data = self.percy_client.create_build(repo='foo/bar', resources=resources) commit_data = self.percy_client.environment.commit_data assert mock.request_history[0].json() == { 'data': { 'type': 'builds', 'attributes': { 'branch': self.percy_client.environment.branch, 'target-branch': self.percy_client.environment.target_branch, 'commit-sha': commit_data['sha'], 'commit-committed-at': commit_data['committed_at'], 'commit-author-name': commit_data['author_name'], 'commit-author-email': commit_data['author_email'], 'commit-committer-name': commit_data['committer_name'], 'commit-committer-email': commit_data['committer_email'], 'commit-message': commit_data['message'], 'pull-request-number': self.percy_client.environment.pull_request_number, 'parallel-nonce': self.percy_client.environment.parallel_nonce, 'parallel-total-shards': self.percy_client.environment.parallel_total_shards, }, 'relationships': { 'resources': { 'data': [{ 'type': 'resources', 'id': resources[0].sha, 'attributes': { 'resource-url': resources[0].resource_url, 'mimetype': resources[0].mimetype, 'is-root': resources[0].is_root, } }], } } } } assert build_data == json.loads(build_fixture)
def snapshot_resources(self): # Only one snapshot resource, the root page HTML. return [ percy.Resource( # Assumes a Selenium webdriver interface. resource_url=urlparse(self.webdriver.current_url).path, is_root=True, mimetype='text/html', content=self.webdriver.page_source, ) ]
def test_create_snapshot(self, mock): fixture_path = os.path.join(FIXTURES_DIR, 'build_response.json') build_fixture = open(fixture_path).read() mock.post('https://percy.io/api/v1/repos/foo/bar/builds/', text=build_fixture) build_id = json.loads(build_fixture)['data']['id'] resources = [ percy.Resource(resource_url='/', is_root=True, content='foo'), ] fixture_path = os.path.join(FIXTURES_DIR, 'snapshot_response.json') mock_data = open(fixture_path).read() mock.post( 'https://percy.io/api/v1/builds/{0}/snapshots/'.format(build_id), text=mock_data) build_data = self.percy_client.create_build(repo='foo/bar') snapshot_data = self.percy_client.create_snapshot( build_id, resources, name='homepage', widths=[1280], enable_javascript=True, ) assert mock.request_history[1].json() == { 'data': { 'type': 'snapshots', 'attributes': { 'name': 'homepage', 'enable-javascript': True, 'widths': [1280], }, 'relationships': { 'resources': { 'data': [{ 'type': 'resources', 'id': resources[0].sha, 'attributes': { 'resource-url': resources[0].resource_url, 'mimetype': resources[0].mimetype, 'is-root': resources[0].is_root, } }], } } } } assert snapshot_data == json.loads(mock_data)
def test_create_build(self, mock): fixture_path = os.path.join(FIXTURES_DIR, 'build_response.json') build_fixture = open(fixture_path).read() mock.post('https://percy.io/api/v1/repos/foo/bar/builds/', text=build_fixture) resources = [ percy.Resource(resource_url='/main.css', is_root=False, content='foo'), ] build_data = self.percy_client.create_build(repo='foo/bar', resources=resources) assert mock.request_history[0].json() == { 'data': { 'type': 'builds', 'attributes': { 'branch': self.percy_client.environment.branch, 'commit-sha': self.percy_client.environment.commit_sha, 'pull-request-number': self.percy_client.environment.pull_request_number, }, 'relationships': { 'resources': { 'data': [{ 'type': 'resources', 'id': resources[0].sha, 'attributes': { 'resource-url': resources[0].resource_url, 'mimetype': resources[0].mimetype, 'is-root': resources[0].is_root, } }], } } } } assert build_data == json.loads(build_fixture)