Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(touch): add coverage for -d option #925

Merged
merged 1 commit into from Jan 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 7 additions & 6 deletions README.md
Expand Up @@ -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).


Expand Down
13 changes: 7 additions & 6 deletions src/touch.js
Expand Up @@ -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) {
Expand Down
11 changes: 11 additions & 0 deletions test/touch.js
Expand Up @@ -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);
Expand Down