-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add timezone input field (#19);
fix: when no utcOffset is passed, force it to be set to UTC+0; test: rewrite test code;
- Loading branch information
Showing
6 changed files
with
160 additions
and
83 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,37 @@ | ||
const core = require("@actions/core"); | ||
const moment = require('moment'); | ||
const core = require('@actions/core') | ||
const moment = require('moment-timezone') | ||
|
||
function action () { | ||
function action() { | ||
try { | ||
const utcOffset = core.getInput('utcOffset', { required: false }); | ||
const format = core.getInput('format', { required: false }); | ||
const utcOffset = core.getInput('utcOffset', {required: false}) | ||
const format = core.getInput('format', {required: false}) | ||
const timezone = core.getInput('timezone', {required: false}) | ||
|
||
const time = moment().utcOffset(utcOffset); | ||
let time = moment() | ||
if (timezone) | ||
time = time.tz(timezone) | ||
else if (utcOffset) | ||
time = time.utcOffset(utcOffset) | ||
else | ||
time = time.utcOffset('+00:00') | ||
|
||
core.setOutput("time", time.toISOString()); | ||
core.setOutput("ISOTime", time.toISOString()); | ||
core.setOutput("readableTime", time.toString()); | ||
core.setOutput("formattedTime", time.format(format)); | ||
core.setOutput('time', time.toISOString()) | ||
core.setOutput('ISOTime', time.toISOString()) | ||
core.setOutput('readableTime', time.toString()) | ||
core.setOutput('formattedTime', time.format(format)) | ||
|
||
let [year, month, day, hour, minute, second, millisecond] = time.toArray(); | ||
month = String(Number(month) + 1); | ||
core.setOutput("year", year) | ||
core.setOutput("month", month) | ||
core.setOutput("day", day) | ||
core.setOutput("hour", hour) | ||
core.setOutput("minute", minute) | ||
core.setOutput("second", second) | ||
core.setOutput("millisecond", millisecond) | ||
let [year, month, day, hour, minute, second, millisecond] = time.toArray() | ||
month = Number(month) + 1 | ||
core.setOutput('year', year) | ||
core.setOutput('month', month) | ||
core.setOutput('day', day) | ||
core.setOutput('hour', hour) | ||
core.setOutput('minute', minute) | ||
core.setOutput('second', second) | ||
core.setOutput('millisecond', millisecond) | ||
} catch (error) { | ||
core.setFailed(error.message); | ||
core.setFailed(error.message) | ||
} | ||
} | ||
|
||
module.exports = action; | ||
module.exports = action |
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 |
---|---|---|
@@ -1,59 +1,108 @@ | ||
const mockMoment = { | ||
utcOffset: jest.fn().mockReturnThis(), | ||
format: jest.fn(), | ||
toISOString: jest.fn(), | ||
toString: jest.fn(), | ||
toArray: jest.fn() | ||
}; | ||
jest.doMock('moment', () => () => mockMoment); | ||
|
||
const mockCore = { | ||
getInput: jest.fn(), | ||
setOutput: jest.fn(), | ||
setFailed: jest.fn(), | ||
}; | ||
jest.doMock('@actions/core', () => mockCore); | ||
setFailed: jest.fn() | ||
} | ||
jest.doMock('@actions/core', () => mockCore) | ||
|
||
jest.spyOn(Date, 'now').mockReturnValue(new Date('2020-07-01T00:30:15.000Z')) | ||
|
||
const action = require('./action.js'); | ||
const action = require('./action.js') | ||
|
||
describe("action", () => { | ||
it("Should load", () => { | ||
expect(action).not.toBeNull(); | ||
}); | ||
describe('action', () => { | ||
it('Should load', () => { | ||
expect(action).not.toBeNull() | ||
}) | ||
|
||
it("Should run with basic functionality", () => { | ||
mockMoment.toISOString.mockReturnValue('##'); | ||
mockMoment.toString.mockReturnValue('##'); | ||
mockMoment.format.mockReturnValue('##'); | ||
action(); | ||
expect(mockCore.setOutput).toHaveBeenCalledWith('time', '##'); | ||
expect(mockCore.setOutput).toHaveBeenCalledWith('ISOTime', '##'); | ||
expect(mockCore.setOutput).toHaveBeenCalledWith('readableTime', '##'); | ||
expect(mockCore.setOutput).toHaveBeenCalledWith('formattedTime', '##'); | ||
}); | ||
it('Should correctly set outputs', () => { | ||
jest.clearAllMocks() | ||
mockCore.getInput = jest.fn().mockImplementation((input) => { | ||
switch (input) { | ||
case 'utcOffset': | ||
return '' | ||
case 'format': | ||
return '' | ||
case 'timezone': | ||
return '' | ||
} | ||
}) | ||
action() | ||
expect(mockCore.setOutput.mock.calls).toEqual([ | ||
['time', '2020-07-01T00:30:15.000Z'], | ||
['ISOTime', '2020-07-01T00:30:15.000Z'], | ||
['readableTime', 'Wed Jul 01 2020 00:30:15 GMT+0000'], | ||
['formattedTime', '2020-07-01T00:30:15Z'], | ||
['year', 2020], | ||
['month', 7], | ||
['day', 1], | ||
['hour', 0], | ||
['minute', 30], | ||
['second', 15], | ||
['millisecond', 0] | ||
]) | ||
}) | ||
|
||
it("Should run with other basic outputs", () => { | ||
mockMoment.toArray.mockReturnValue(['2020', '0', '1', '12', '0', '1', '2']); | ||
action(); | ||
expect(mockCore.setOutput).toHaveBeenCalledWith('year', '2020'); | ||
expect(mockCore.setOutput).toHaveBeenCalledWith('month', '1'); | ||
expect(mockCore.setOutput).toHaveBeenCalledWith('day', '1'); | ||
expect(mockCore.setOutput).toHaveBeenCalledWith('hour', '12'); | ||
expect(mockCore.setOutput).toHaveBeenCalledWith('minute', '0'); | ||
expect(mockCore.setOutput).toHaveBeenCalledWith('second', '1'); | ||
expect(mockCore.setOutput).toHaveBeenCalledWith('millisecond', '2'); | ||
}); | ||
it('Should correctly set outputs with utcOffset', () => { | ||
jest.clearAllMocks() | ||
mockCore.getInput = jest.fn().mockImplementation((input) => { | ||
switch (input) { | ||
case 'utcOffset': | ||
return '+08:00' | ||
case 'format': | ||
return 'YYYYMMDD-HH' | ||
case 'timezone': | ||
return '' | ||
} | ||
}) | ||
action() | ||
expect(mockCore.setOutput.mock.calls).toEqual([ | ||
['time', '2020-07-01T00:30:15.000Z'], | ||
['ISOTime', '2020-07-01T00:30:15.000Z'], | ||
['readableTime', 'Wed Jul 01 2020 08:30:15 GMT+0800'], | ||
['formattedTime', '20200701-08'], | ||
['year', 2020], | ||
['month', 7], | ||
['day', 1], | ||
['hour', 8], | ||
['minute', 30], | ||
['second', 15], | ||
['millisecond', 0] | ||
]) | ||
}) | ||
|
||
it("Should pass format inputs", () => { | ||
mockCore.getInput.mockReturnValue('###'); | ||
action(); | ||
expect(mockMoment.utcOffset).toHaveBeenCalledWith('###'); | ||
expect(mockMoment.format).toHaveBeenCalledWith('###'); | ||
}); | ||
it('Should correctly set outputs with timezone', () => { | ||
jest.clearAllMocks() | ||
mockCore.getInput = jest.fn().mockImplementation((input) => { | ||
switch (input) { | ||
case 'utcOffset': | ||
return '+08:00' | ||
case 'format': | ||
return 'YYYYMMDD-HH' | ||
case 'timezone': | ||
return 'America/Los_Angeles' | ||
} | ||
}) | ||
action() | ||
expect(mockCore.setOutput.mock.calls).toEqual([ | ||
['time', '2020-07-01T00:30:15.000Z'], | ||
['ISOTime', '2020-07-01T00:30:15.000Z'], | ||
['readableTime', 'Tue Jun 30 2020 17:30:15 GMT-0700'], | ||
['formattedTime', '20200630-17'], | ||
['year', 2020], | ||
['month', 6], | ||
['day', 30], | ||
['hour', 17], | ||
['minute', 30], | ||
['second', 15], | ||
['millisecond', 0] | ||
]) | ||
}) | ||
|
||
it("Should throw error", () => { | ||
mockMoment.utcOffset = () => { throw new Error('####') } | ||
action(); | ||
expect(mockCore.setFailed).toHaveBeenCalledWith('####'); | ||
}); | ||
}); | ||
it('Should throw error', () => { | ||
jest.clearAllMocks() | ||
mockCore.setOutput = () => { | ||
throw new Error('#') | ||
} | ||
action() | ||
expect(mockCore.setFailed).toHaveBeenCalledWith('#') | ||
}) | ||
}) |
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
const action = require('./action.js'); | ||
action(); | ||
const action = require('./action.js') | ||
action() |
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