Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
juan-fernandez committed Oct 2, 2024
1 parent 4801a15 commit cd30a5d
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/dd-trace/src/plugins/util/ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,10 @@ module.exports = {
})
}
if (GITHUB_BASE_REF) { // `pull_request` or `pull_request_target` event
tags[GIT_PULL_REQUEST_BASE_BRANCH] = GITHUB_BASE_REF
try {
const eventContent = getGitHubEventPayload()
tags[GIT_PULL_REQUEST_BASE_BRANCH_SHA] = eventContent.pull_request.base.sha
tags[GIT_PULL_REQUEST_BASE_BRANCH] = GITHUB_BASE_REF
tags[GIT_COMMIT_HEAD_SHA] = eventContent.pull_request.head.sha
} catch (e) {
// ignore malformed event content
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"action": "synchronize",
"after": "df289512a51123083a8e6931dd6f57bb3883d4c4",
"before": "f659d2fdd7bedffb40d9ab223dbde6afa5eadc32",
"number": 1,
"pull_request": {
"_links": {},
"active_lock_reason": null,
"additions": 2,
"assignee": null,
"assignees": [],
"author_association": "OWNER",
"auto_merge": null,
"base": {
"label": "datadog:main",
"ref": "main",
"repo": {},
"sha": "52e0974c74d41160a03d59ddc73bb9f5adab054b",
"user": {}
},
"body": "# What Does This Do\r\n\r\n# Motivation\r\n\r\n# Additional Notes\r\n",
"changed_files": 3,
"closed_at": null,
"comments": 0,
"comments_url": "",
"commits": 2,
"commits_url": "",
"created_at": "2024-09-11T15:08:02Z",
"deletions": 0,
"diff_url": "",
"draft": false,
"head": {
"label": "forked_org:test-branch",
"ref": "test-branch",
"repo": {},
"sha": "df289512a51123083a8e6931dd6f57bb3883d4c4",
"user": {}
},
"html_url": "",
"id": 2066570986,
"issue_url": "",
"labels": [],
"locked": false,
"maintainer_can_modify": false,
"merge_commit_sha": "d9a3212d0d5d1483426dbbdf0beea32ee50abcde",
"mergeable": null,
"mergeable_state": "unknown",
"merged": false,
"merged_at": null,
"merged_by": null,
"milestone": null,
"node_id": "PR_kwDOIvpGAs57LV7q",
"number": 1,
"patch_url": "",
"rebaseable": null,
"requested_reviewers": [],
"requested_teams": [],
"review_comment_url": "",
"review_comments": 0,
"review_comments_url": "",
"state": "open",
"statuses_url": "",
"title": "Test commit",
"updated_at": "2024-09-11T15:12:26Z",
"url": "",
"user": {}
},
"repository": {},
"sender": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
46 changes: 45 additions & 1 deletion packages/dd-trace/test/plugins/util/test-environment.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@ const proxyquire = require('proxyquire')
const execFileSyncStub = sinon.stub().returns('')

const { getCIMetadata } = require('../../../src/plugins/util/ci')
const { CI_ENV_VARS, CI_NODE_LABELS } = require('../../../src/plugins/util/tags')
const {
CI_ENV_VARS,
CI_NODE_LABELS,
GIT_PULL_REQUEST_BASE_BRANCH,
GIT_PULL_REQUEST_BASE_BRANCH_SHA,
GIT_COMMIT_HEAD_SHA
} = require('../../../src/plugins/util/tags')

const { getGitMetadata } = proxyquire('../../../src/plugins/util/git', {
child_process: {
Expand All @@ -36,6 +42,44 @@ describe('test environment data', () => {
const ciProviders = fs.readdirSync(path.join(__dirname, 'ci-env'))
ciProviders.forEach(ciProvider => {
const assertions = require(path.join(__dirname, 'ci-env', ciProvider))
if (ciProvider === 'github.json') {
// We grab the first assertion because we only need to test one
const [env] = assertions[0]
it('can read pull request data from GitHub Actions', () => {
process.env = env
process.env.GITHUB_BASE_REF = 'datadog:main'
process.env.GITHUB_EVENT_PATH = path.join(__dirname, 'fixtures', 'github_event_payload.json')
const {
[GIT_PULL_REQUEST_BASE_BRANCH]: pullRequestBaseBranch,
[GIT_PULL_REQUEST_BASE_BRANCH_SHA]: pullRequestBaseBranchSha,
[GIT_COMMIT_HEAD_SHA]: headCommitSha
} = getTestEnvironmentMetadata()

expect({
pullRequestBaseBranch,
pullRequestBaseBranchSha,
headCommitSha
}).to.eql({
pullRequestBaseBranch: 'datadog:main',
pullRequestBaseBranchSha: '52e0974c74d41160a03d59ddc73bb9f5adab054b',
headCommitSha: 'df289512a51123083a8e6931dd6f57bb3883d4c4'
})
})
it('does not crash if GITHUB_EVENT_PATH is not a valid JSON file', () => {
process.env = env
process.env.GITHUB_BASE_REF = 'datadog:main'
process.env.GITHUB_EVENT_PATH = path.join(__dirname, 'fixtures', 'github_event_payload_malformed.json')
const {
[GIT_PULL_REQUEST_BASE_BRANCH]: pullRequestBaseBranch,
[GIT_PULL_REQUEST_BASE_BRANCH_SHA]: pullRequestBaseBranchSha,
[GIT_COMMIT_HEAD_SHA]: headCommitSha
} = getTestEnvironmentMetadata()

expect(pullRequestBaseBranch).to.equal('datadog:main')
expect(pullRequestBaseBranchSha).to.be.undefined
expect(headCommitSha).to.be.undefined
})
}

assertions.forEach(([env, expectedSpanTags], index) => {
it(`reads env info for spec ${index} from ${ciProvider}`, () => {
Expand Down

0 comments on commit cd30a5d

Please sign in to comment.