Skip to content

Commit 7125b68

Browse files
authored
Merge pull request #60 from Techn1x/improved-regex
Improved regex, added new flags to helper
2 parents d144d77 + 261716a commit 7125b68

File tree

6 files changed

+136
-41
lines changed

6 files changed

+136
-41
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ This addon provides `{{app-version}}` helper that allows you to show your curren
1111

1212
The addon has flags to display parts of the version:
1313

14-
* `{{app-version hideSha=true}} // => 2.0.1`
15-
* `{{app-version hideVersion=true}} // => <git SHA>`
14+
* `{{app-version versionOnly=true}} // => 2.0.1`
15+
* `{{app-version versionOnly=true showExtended=true}} // => 2.0.1-alpha.1`
16+
* `{{app-version shaOnly=true}} // => <git SHA>`
1617

1718
Flags are `false` by default.
1819

addon/utils/regexp.js

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/helpers/app-version.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
11
import { helper } from '@ember/component/helper';
22
import config from '../config/environment';
3-
import { shaRegExp, versionRegExp } from 'ember-cli-app-version/utils/regexp';
4-
5-
const {
6-
APP: {
7-
version
8-
}
9-
} = config;
3+
import { shaRegExp, versionRegExp, versionExtendedRegExp } from 'ember-cli-app-version/utils/regexp';
104

115
export function appVersion(_, hash = {}) {
12-
if (hash.hideSha) {
13-
return version.match(versionRegExp)[0];
6+
const version = config.APP.version;
7+
// e.g. 1.0.0-alpha.1+4jds75hf
8+
9+
// Allow use of 'hideSha' and 'hideVersion' For backwards compatibility
10+
let versionOnly = hash.versionOnly || hash.hideSha;
11+
let shaOnly = hash.shaOnly || hash.hideVersion;
12+
13+
let match = null;
14+
15+
if (versionOnly) {
16+
if (hash.showExtended) {
17+
match = version.match(versionExtendedRegExp); // 1.0.0-alpha.1
18+
}
19+
// Fallback to just version
20+
if (!match) {
21+
match = version.match(versionRegExp); // 1.0.0
22+
}
1423
}
1524

16-
if (hash.hideVersion) {
17-
return version.match(shaRegExp)[0];
25+
if (shaOnly) {
26+
match = version.match(shaRegExp); // 4jds75hf
1827
}
1928

20-
return version;
29+
return match ? match[0] : version;
2130
}
2231

2332
export default helper(appVersion);
Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,54 @@
11
import { moduleForComponent, test } from 'ember-qunit';
22
import hbs from 'htmlbars-inline-precompile';
3-
import { shaRegExp, versionRegExp } from 'ember-cli-app-version/utils/regexp';
43

54
moduleForComponent('Integration | Helper | {{app-version}}', {
65
integration: true
76
});
87

9-
test('it displays only app version', function(assert) {
10-
assert.expect(2);
8+
test('it displays entire version', function(assert) {
9+
assert.expect(1);
10+
11+
this.render(hbs`{{app-version}}`);
12+
13+
assert.ok(this.$().text(), 'Version not empty');
14+
});
15+
16+
test('it displays only app version (backwards compatible)', function(assert) {
17+
assert.expect(1);
1118

1219
this.render(hbs`{{app-version hideSha=true}}`);
1320

14-
assert.ok(this.$().text().match(versionRegExp), 'Displays version.');
15-
assert.ok(!this.$().text().match(shaRegExp), 'Does not display git sha.');
21+
assert.ok(this.$().text(), 'Version not empty');
1622
});
1723

18-
test('it displays only git sha', function(assert) {
19-
assert.expect(2);
24+
test('it displays only app version', function(assert) {
25+
assert.expect(1);
26+
27+
this.render(hbs`{{app-version versionOnly=true}}`);
28+
29+
assert.ok(this.$().text(), 'Version not empty');
30+
});
31+
32+
test('it displays only app version extended', function(assert) {
33+
assert.expect(1);
34+
35+
this.render(hbs`{{app-version versionOnly=true showExtended=true}}`);
36+
37+
assert.ok(this.$().text(), 'Version not empty');
38+
});
39+
40+
test('it displays only git sha (backwards compatible)', function(assert) {
41+
assert.expect(1);
2042

2143
this.render(hbs`{{app-version hideVersion=true}}`);
2244

23-
assert.ok(this.$().text().match(shaRegExp), 'Displays git sha.');
24-
assert.ok(!this.$().text().match(versionRegExp), 'Does not display version.');
45+
assert.ok(this.$().text(), 'Version not empty');
2546
});
47+
48+
test('it displays only git sha', function(assert) {
49+
assert.expect(1);
50+
51+
this.render(hbs`{{app-version shaOnly=true}}`);
52+
53+
assert.ok(this.$().text(), 'Version not empty');
54+
});
Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,78 @@
11
import { appVersion } from 'dummy/helpers/app-version';
2-
import { module, test } from 'qunit';
32
import config from 'dummy/config/environment';
3+
import { module, test } from 'qunit';
44

5-
const {
6-
APP: {
7-
version
8-
}
9-
} = config;
5+
const versionOnlyString = '10.20.3';
6+
const extendedTagOnlyString = 'alpha.15';
7+
const shaOnlyString = 'deadb33f';
8+
9+
const versionString = versionOnlyString+'-'+extendedTagOnlyString+'+'+shaOnlyString;
10+
const standardVersionString = versionOnlyString+'+'+shaOnlyString;
11+
const oldVersion = config.APP.version;
1012

11-
module('Unit | Helper | app version');
13+
module('Unit | Helper | app version', {
14+
afterEach() {
15+
config.APP.version = oldVersion;
16+
}
17+
});
1218

1319
test('it returns app version', function(assert) {
1420
assert.expect(1);
21+
config.APP.version = versionString;
1522

16-
let result = appVersion();
23+
assert.equal(appVersion(), versionString, 'Returns app version.');
24+
});
1725

18-
assert.equal(result, version, 'Returns app version.');
26+
test('it returns only app version (backwards compatible)', function(assert) {
27+
assert.expect(1);
28+
29+
config.APP.version = versionString;
30+
let result = appVersion([], { hideSha: true });
31+
32+
assert.equal(result, versionOnlyString, 'Returns app version without git sha.');
1933
});
2034

2135
test('it returns only app version', function(assert) {
2236
assert.expect(1);
2337

24-
let result = appVersion([], { hideSha: true });
38+
config.APP.version = versionString;
39+
let result = appVersion([], { versionOnly: true });
2540

26-
assert.equal(result, version.split('+')[0], 'Returns app version without git sha.');
41+
assert.equal(result, versionOnlyString, 'Returns app version without git sha.');
2742
});
2843

29-
test('it returns only git sha', function(assert) {
44+
test('it returns only app version extended', function(assert) {
3045
assert.expect(1);
3146

47+
config.APP.version = versionString;
48+
let result = appVersion([], { versionOnly: true, showExtended: true });
49+
50+
assert.equal(result, versionOnlyString+'-'+extendedTagOnlyString, 'Returns app version extended without git sha.');
51+
});
52+
53+
test('it returns only app version (falls back when no extended)', function(assert) {
54+
assert.expect(1);
55+
56+
config.APP.version = standardVersionString;
57+
let result = appVersion([], { versionOnly: true, showExtended: true });
58+
59+
assert.equal(result, versionOnlyString, 'Returns app version without git sha.');
60+
});
61+
62+
test('it returns only git sha (backwards compatible)', function(assert) {
63+
assert.expect(1);
64+
65+
config.APP.version = versionString;
3266
let result = appVersion([], { hideVersion: true });
3367

34-
assert.equal(result, version.split('+')[1], 'Returns git sha without app version.');
68+
assert.equal(result, shaOnlyString, 'Returns git sha without app version.');
69+
});
70+
71+
test('it returns only git sha', function(assert) {
72+
assert.expect(1);
73+
74+
config.APP.version = versionString;
75+
let result = appVersion([], { shaOnly: true });
76+
77+
assert.equal(result, shaOnlyString, 'Returns git sha without app version.');
3578
});

tests/unit/utils/regexp-test.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
11
import { module, test } from 'qunit';
2-
import { shaRegExp, versionRegExp } from 'ember-cli-app-version/utils/regexp';
2+
import { shaRegExp, versionRegExp, versionExtendedRegExp } from 'ember-cli-app-version/utils/regexp';
33

44
module('Unit | Utility | regexp');
55

66
test('version reg ex matches expected strings', function(assert) {
7-
assert.expect(3);
7+
assert.expect(4);
88

99
assert.ok('2.0.1'.match(versionRegExp), 'Matches expected pattern.');
10+
assert.ok('2.20.1'.match(versionRegExp), 'Matches expected pattern.');
1011
assert.ok(!'a.b.c'.match(versionRegExp), 'Does not match letters.');
1112
assert.ok(!'git12sha'.match(versionRegExp), 'Does not match sha.');
1213
});
1314

15+
test('version extended reg ex matches expected strings', function(assert) {
16+
assert.expect(4);
17+
18+
assert.ok('2.0.1-alpha'.match(versionExtendedRegExp), 'Matches expected pattern.');
19+
assert.ok('2.20.1-alpha.15'.match(versionExtendedRegExp), 'Matches expected pattern.');
20+
assert.ok(!'a.b.c-alpha.15'.match(versionExtendedRegExp), 'Does not match letters.');
21+
assert.ok(!'git12sha'.match(versionExtendedRegExp), 'Does not match sha.');
22+
});
23+
1424
test('git sha reg ex matches expected strings', function(assert) {
15-
assert.expect(2);
25+
assert.expect(4);
1626

1727
assert.ok('git12sha'.match(shaRegExp), 'Matches expected pattern.');
1828
assert.ok(!'2.0.1'.match(shaRegExp), 'Does not match version pattern.');
29+
assert.ok(!'2.0.1-alpha.15'.match(shaRegExp), 'Does not match version extended pattern.');
30+
assert.ok(!'2.0.1-alphaabc.15'.match(shaRegExp), 'Does not match version extended pattern (with 8 chars in tag name).');
1931
});

0 commit comments

Comments
 (0)