From e1fb25acdba12ce6a069fb09115014f2f679f173 Mon Sep 17 00:00:00 2001 From: Nate Fischer Date: Thu, 3 Jan 2019 23:39:18 -0800 Subject: [PATCH] test(touch): add coverage for -d option No change to production logic. We never had coverage for `touch({'-d': ...})`, so this adds test coverage. This also updates documentation to clarify the parameter should be an instance of the `Date` type. Test: `handles date argument` case --- README.md | 13 +++++++------ src/touch.js | 13 +++++++------ test/touch.js | 11 +++++++++++ 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index fde0b443..04879742 100644 --- a/README.md +++ b/README.md @@ -647,19 +647,20 @@ Available options: + `-a`: Change only the access time + `-c`: Do not create any files + `-m`: Change only the modification time -+ `-d DATE`: Parse `DATE` and use it instead of current time -+ `-r FILE`: Use `FILE`'s times instead of current time ++ `{'-d': date}`: Use `date` (instance of `Date`) instead of current time ++ `{'-r': file}`: Use `file`'s times instead of current time Examples: ```javascript touch('source.js'); -touch('-c', '/path/to/some/dir/source.js'); -touch({ '-r': FILE }, '/path/to/some/dir/source.js'); +touch('-c', 'path/to/file.js'); +touch({ '-r': 'referenceFile.txt' }, 'path/to/file.js'); +touch({ '-d': new Date('December 17, 1995 03:24:00') }, 'path/to/file.js'); ``` -Update the access and modification times of each `FILE` to the current time. -A `FILE` argument that does not exist is created empty, unless `-c` is supplied. +Update the access and modification times of each file to the current time. +A file argument that does not exist is created empty, unless `-c` is supplied. This is a partial implementation of [`touch(1)`](http://linux.die.net/man/1/touch). diff --git a/src/touch.js b/src/touch.js index 7b7033cd..a8611b5c 100644 --- a/src/touch.js +++ b/src/touch.js @@ -20,19 +20,20 @@ common.register('touch', _touch, { //@ + `-a`: Change only the access time //@ + `-c`: Do not create any files //@ + `-m`: Change only the modification time -//@ + `-d DATE`: Parse `DATE` and use it instead of current time -//@ + `-r FILE`: Use `FILE`'s times instead of current time +//@ + `{'-d': date}`: Use `date` (instance of `Date`) instead of current time +//@ + `{'-r': file}`: Use `file`'s times instead of current time //@ //@ Examples: //@ //@ ```javascript //@ touch('source.js'); -//@ touch('-c', '/path/to/some/dir/source.js'); -//@ touch({ '-r': FILE }, '/path/to/some/dir/source.js'); +//@ touch('-c', 'path/to/file.js'); +//@ touch({ '-r': 'referenceFile.txt' }, 'path/to/file.js'); +//@ touch({ '-d': new Date('December 17, 1995 03:24:00') }, 'path/to/file.js'); //@ ``` //@ -//@ Update the access and modification times of each `FILE` to the current time. -//@ A `FILE` argument that does not exist is created empty, unless `-c` is supplied. +//@ Update the access and modification times of each file to the current time. +//@ A file argument that does not exist is created empty, unless `-c` is supplied. //@ This is a partial implementation of [`touch(1)`](http://linux.die.net/man/1/touch). function _touch(opts, files) { if (!files) { diff --git a/test/touch.js b/test/touch.js index 41b2107b..32e283e8 100644 --- a/test/touch.js +++ b/test/touch.js @@ -119,6 +119,17 @@ test('uses a reference file for mtime', t => { ); }); +test('accepts -d flag', t => { + const testFile = tmpFile(t); + const oldStat = resetUtimes(testFile); + const date = new Date('December 17, 1995 03:24:00'); + const result = shell.touch({'-d': date}, testFile); + t.is(result.code, 0); + // Compare getTime(), because Date can't be compared with triple-equals. + t.is(common.statFollowLinks(testFile).mtime.getTime(), date.getTime()); + t.is(common.statFollowLinks(testFile).atime.getTime(), date.getTime()); +}); + test('sets mtime and atime by default', t => { const testFile = tmpFile(t); const oldStat = resetUtimes(testFile);