Skip to content

Commit 7b1aa70

Browse files
yaroslav-codefreshyaroslav-codefresh
authored andcommitted
add no pretty flag date format case + add tests (#274)
1 parent f737d06 commit 7b1aa70

File tree

4 files changed

+107
-3
lines changed

4 files changed

+107
-3
lines changed

lib/output/Formatter.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Formatter {
1414
const obj = Object.assign({}, source);
1515
keys.forEach((key) => {
1616
const prop = obj[key];
17-
obj[key] = this._applyStyles(key, prop);
17+
obj[key] = this.applyStyles(key, prop);
1818
});
1919
return obj;
2020
}
@@ -26,7 +26,7 @@ class Formatter {
2626
return this;
2727
}
2828

29-
_applyStyles(key, prop) {
29+
applyStyles(key, prop) {
3030
const style = this.styles[key] || _.identity;
3131
return style(prop);
3232
}

lib/output/types/table.output.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,17 @@ function output(data, argv) {
8080
info = _.mapValues(info, dateFormatter);
8181
}
8282

83+
// if no pretty flag and no dateFormat - force apply specific entity date format
84+
if (!pretty && (!dateFormat || dateFormat === 'default')) {
85+
const formatter = FormatterRegistry.get(entities[0].constructor.name);
86+
_.keys(info).forEach((key) => {
87+
const prop = info[key];
88+
if (prop instanceof Date) {
89+
info[key] = formatter.applyStyles(key, prop);
90+
}
91+
});
92+
}
93+
8394
if (pretty) {
8495
const formatter = FormatterRegistry.get(entities[0].constructor.name);
8596
info = formatter.apply(info);

lib/output/types/table.output.spec.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
const _ = require('lodash');
2+
3+
const { Formatter } = require('../Formatter');
4+
const Style = require('../Style');
5+
const output = require('./table.output');
6+
const Entity = require('../../logic/entities/Entity');
7+
8+
const mockDefaultEntityDateFormatter = Formatter.build()
9+
.style('date', Style.date);
10+
11+
const TESTED_DATE = '2019-01-16';
12+
const TESTED_TIME = '02:00:00';
13+
const TESTED_DATETIME = `${TESTED_DATE}, ${TESTED_TIME}`;
14+
const TESTED_DATE_OBJ = new Date(TESTED_DATETIME);
15+
let mockConfig = {};
16+
17+
jest.mock('../../logic/cli-config/Manager', () => {
18+
return {
19+
config: () => (mockConfig),
20+
};
21+
});
22+
23+
24+
jest.mock('../formatters/index', () => {
25+
const get = () => mockDefaultEntityDateFormatter;
26+
return {
27+
FormatterRegistry: { get },
28+
};
29+
});
30+
31+
class TestEntity extends Entity {
32+
constructor(data) {
33+
super();
34+
this.info = data;
35+
this.defaultColumns = ['date'];
36+
}
37+
38+
}
39+
40+
41+
// todo: add other tests on coverage
42+
describe('table output', () => {
43+
describe('date format', () => {
44+
it('should apply default entity date format when no pretty flag and no date format are passed', () => {
45+
const argv = {}; // no pretty flag
46+
mockConfig = { output: { pretty: false, dateFormat: 'default' } };
47+
const table = output(new TestEntity({ date: TESTED_DATE_OBJ }), argv);
48+
expect(table.split('\n')[1]).toBe(TESTED_DATE);
49+
});
50+
51+
it('should apply default entity date format when pretty flag and no date format are passed', () => {
52+
const argv = { pretty: true };
53+
mockConfig = { output: { pretty: false, dateFormat: 'default' } };
54+
const table = output(new TestEntity({ date: TESTED_DATE_OBJ }), argv);
55+
expect(table.split('\n')[1]).toBe(TESTED_DATE);
56+
});
57+
58+
it('should apply default entity date format when pretty flag on cli-config and no date format is passed', () => {
59+
const argv = {};
60+
mockConfig = { output: { pretty: true, dateFormat: 'default' } };
61+
const table = output(new TestEntity({ date: TESTED_DATE_OBJ }), argv);
62+
expect(table.split('\n')[1]).toBe(TESTED_DATE);
63+
});
64+
65+
it('should apply date format when it\'s passed', () => {
66+
const argv = { dateFormat: 'datetime' };
67+
mockConfig = { output: { pretty: false, dateFormat: 'default' } };
68+
const table = output(new TestEntity({ date: TESTED_DATE_OBJ }), argv);
69+
expect(table.split('\n')[1]).toBe(TESTED_DATETIME);
70+
});
71+
72+
it('should apply date format when it\'s inside cli-config and not at argv', () => {
73+
const argv = {};
74+
mockConfig = { output: { pretty: false, dateFormat: 'datetime' } };
75+
const table = output(new TestEntity({ date: TESTED_DATE_OBJ }), argv);
76+
expect(table.split('\n')[1]).toBe(TESTED_DATETIME);
77+
});
78+
79+
it('should apply date format at argv over cli-config date-format', () => {
80+
const argv = { dateFormat: 'date' };
81+
mockConfig = { output: { pretty: false, dateFormat: 'datetime' } };
82+
const table = output(new TestEntity({ date: TESTED_DATE_OBJ }), argv);
83+
expect(table.split('\n')[1]).toBe(TESTED_DATE);
84+
});
85+
86+
it('should be able to apply custom date-format', () => {
87+
const argv = { dateFormat: 'YYYY-MM' };
88+
mockConfig = { output: { pretty: false, dateFormat: 'datetime' } };
89+
const table = output(new TestEntity({ date: TESTED_DATE_OBJ }), argv);
90+
expect(table.split('\n')[1]).toBe('2019-01');
91+
});
92+
});
93+
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "codefresh",
3-
"version": "0.10.2",
3+
"version": "0.10.3",
44
"description": "Codefresh command line utility",
55
"main": "index.js",
66
"preferGlobal": true,

0 commit comments

Comments
 (0)