-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: 204 ratio is NaN when previous value is 0 (#222)
* extract getRatio function * print 1 when both values are 0 * print +-∞ when divisor is 0
- Loading branch information
Showing
7 changed files
with
268 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
import { buildComment } from '../src/write'; | ||
import { FakedOctokit, fakedRepos } from './fakedOctokit'; | ||
import dedent from 'dedent'; | ||
|
||
interface RepositoryPayloadSubset { | ||
private: boolean; | ||
html_url: string; | ||
} | ||
|
||
const gitHubContext = { | ||
repo: { | ||
repo: 'repo', | ||
owner: 'user', | ||
}, | ||
payload: { | ||
repository: { | ||
private: false, | ||
html_url: 'https://github.com/user/repo', | ||
} as RepositoryPayloadSubset | null, | ||
}, | ||
workflow: 'Workflow name', | ||
}; | ||
|
||
jest.mock('@actions/github', () => ({ | ||
get context() { | ||
return gitHubContext; | ||
}, | ||
getOctokit(token: string) { | ||
return new FakedOctokit(token); | ||
}, | ||
})); | ||
|
||
afterEach(function () { | ||
fakedRepos.clear(); | ||
}); | ||
|
||
describe('buildComment', () => { | ||
it('should build comment when previous benchmarks are 0 and biggerIsBetter returns false', () => { | ||
const comment = buildComment( | ||
'TestSuite', | ||
{ | ||
date: 12345, | ||
commit: { | ||
id: 'testCommitIdCurrent', | ||
author: { | ||
name: 'TestUser', | ||
}, | ||
committer: { | ||
name: 'TestUser', | ||
}, | ||
message: 'Test current commit message', | ||
url: 'https://test.previous.commit.url', | ||
}, | ||
tool: 'cargo', | ||
benches: [ | ||
{ | ||
value: 0, | ||
name: 'TestBench<1>', | ||
unit: 'testUnit', | ||
}, | ||
{ | ||
value: 1, | ||
name: 'TestBench<2>', | ||
unit: 'testUnit', | ||
}, | ||
{ | ||
value: -1, | ||
name: 'TestBench<3>', | ||
unit: 'testUnit', | ||
}, | ||
], | ||
}, | ||
{ | ||
date: 12345, | ||
commit: { | ||
id: 'testCommitIdPrevious', | ||
author: { | ||
name: 'TestUser', | ||
}, | ||
committer: { | ||
name: 'TestUser', | ||
}, | ||
message: 'Test previous commit message', | ||
url: 'https://test.previous.commit.url', | ||
}, | ||
tool: 'cargo', | ||
benches: [ | ||
{ | ||
value: 0, | ||
name: 'TestBench<1>', | ||
unit: 'testUnit', | ||
}, | ||
{ | ||
value: 0, | ||
name: 'TestBench<2>', | ||
unit: 'testUnit', | ||
}, | ||
{ | ||
value: 0, | ||
name: 'TestBench<3>', | ||
unit: 'testUnit', | ||
}, | ||
], | ||
}, | ||
); | ||
|
||
expect(comment).toEqual(dedent` | ||
# TestSuite | ||
<details> | ||
| Benchmark suite | Current: testCommitIdCurrent | Previous: testCommitIdPrevious | Ratio | | ||
|-|-|-|-| | ||
| \`TestBench<1>\` | \`0\` testUnit | \`0\` testUnit | \`1\` | | ||
| \`TestBench<2>\` | \`1\` testUnit | \`0\` testUnit | \`+∞\` | | ||
| \`TestBench<3>\` | \`-1\` testUnit | \`0\` testUnit | \`-∞\` | | ||
</details> | ||
This comment was automatically generated by [workflow](https://github.com/user/repo/actions?query=workflow%3AWorkflow%20name) using [github-action-benchmark](https://github.com/marketplace/actions/continuous-benchmark). | ||
`); | ||
}); | ||
|
||
it('should build comment when current benchmarks are 0 and biggerIsBetter returns true', () => { | ||
const comment = buildComment( | ||
'TestSuite', | ||
{ | ||
date: 12345, | ||
commit: { | ||
id: 'testCommitIdCurrent', | ||
author: { | ||
name: 'TestUser', | ||
}, | ||
committer: { | ||
name: 'TestUser', | ||
}, | ||
message: 'Test current commit message', | ||
url: 'https://test.previous.commit.url', | ||
}, | ||
tool: 'benchmarkjs', | ||
benches: [ | ||
{ | ||
value: 0, | ||
name: 'TestBench<1>', | ||
unit: 'testUnit', | ||
}, | ||
{ | ||
value: 0, | ||
name: 'TestBench<2>', | ||
unit: 'testUnit', | ||
}, | ||
{ | ||
value: 0, | ||
name: 'TestBench<3>', | ||
unit: 'testUnit', | ||
}, | ||
], | ||
}, | ||
{ | ||
date: 12345, | ||
commit: { | ||
id: 'testCommitIdPrevious', | ||
author: { | ||
name: 'TestUser', | ||
}, | ||
committer: { | ||
name: 'TestUser', | ||
}, | ||
message: 'Test previous commit message', | ||
url: 'https://test.previous.commit.url', | ||
}, | ||
tool: 'benchmarkjs', | ||
benches: [ | ||
{ | ||
value: 0, | ||
name: 'TestBench<1>', | ||
unit: 'testUnit', | ||
}, | ||
{ | ||
value: 1, | ||
name: 'TestBench<2>', | ||
unit: 'testUnit', | ||
}, | ||
{ | ||
value: -1, | ||
name: 'TestBench<3>', | ||
unit: 'testUnit', | ||
}, | ||
], | ||
}, | ||
); | ||
|
||
expect(comment).toEqual(dedent` | ||
# TestSuite | ||
<details> | ||
| Benchmark suite | Current: testCommitIdCurrent | Previous: testCommitIdPrevious | Ratio | | ||
|-|-|-|-| | ||
| \`TestBench<1>\` | \`0\` testUnit | \`0\` testUnit | \`1\` | | ||
| \`TestBench<2>\` | \`0\` testUnit | \`1\` testUnit | \`+∞\` | | ||
| \`TestBench<3>\` | \`0\` testUnit | \`-1\` testUnit | \`-∞\` | | ||
</details> | ||
This comment was automatically generated by [workflow](https://github.com/user/repo/actions?query=workflow%3AWorkflow%20name) using [github-action-benchmark](https://github.com/marketplace/actions/continuous-benchmark). | ||
`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
type OctokitOpts = { owner: string; repo: string; commit_sha: string; body: string }; | ||
class FakedOctokitRepos { | ||
spyOpts: OctokitOpts[]; | ||
constructor() { | ||
this.spyOpts = []; | ||
} | ||
createCommitComment(opt: OctokitOpts) { | ||
this.spyOpts.push(opt); | ||
return Promise.resolve({ | ||
status: 201, | ||
data: { | ||
html_url: 'https://dummy-comment-url', | ||
}, | ||
}); | ||
} | ||
lastCall(): OctokitOpts { | ||
return this.spyOpts[this.spyOpts.length - 1]; | ||
} | ||
clear() { | ||
this.spyOpts = []; | ||
} | ||
} | ||
|
||
export const fakedRepos = new FakedOctokitRepos(); | ||
|
||
export class FakedOctokit { | ||
rest = { | ||
repos: fakedRepos, | ||
}; | ||
opt: { token: string }; | ||
constructor(token: string) { | ||
this.opt = { token }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters