Skip to content

Allow ln destination to be an existing directory, fixes #832 #833

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Allow dead symlinks for source/destination, fixes #829
  • Loading branch information
loilo committed Apr 6, 2018
commit c57f7ffc3ffb8ab828b9032ce6effb477dc5cc30
21 changes: 19 additions & 2 deletions src/ln.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@ function _ln(options, source, dest) {
dest = path.join(dest, path.basename(sourcePath));
}

if (fs.existsSync(dest)) {
var destinationExists;
try {
fs.lstatSync(dest);
destinationExists = true;
} catch (err) {
destinationExists = false;
}

if (destinationExists) {
if (!options.force) {
common.error('Destination file exists', { continue: true });
}
Expand All @@ -51,7 +59,16 @@ function _ln(options, source, dest) {
var isWindows = process.platform === 'win32';
var linkType = isWindows ? 'file' : null;
var resolvedSourcePath = isAbsolute ? sourcePath : path.resolve(process.cwd(), path.dirname(dest), source);
if (!fs.existsSync(resolvedSourcePath)) {

var resolvedSourceExists;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we move this to common.isBrokenLink(nameOfPossibleLink)? This is something I wanted for #835 anyway (and it makes more sense in common than utils). Some ideas for the behavior:

isBrokenLink(pointsToMissingFile)       === true
isBrokenLink(realRegularFile)           // throws an exception
isBrokenLink(someDirectory)             // throws an exception
isBrokenLink(doesNotExist)              // throws an exception
isBrokenLink(pointsToFileOrDirectory)   === false
isBrokenLink(pointsToAnotherBrokenLink) === true // `ls` highlights this just like other broken links

WDYT?

try {
fs.lstatSync(resolvedSourcePath);
resolvedSourceExists = true;
} catch (err) {
resolvedSourceExists = false;
}

if (!resolvedSourceExists) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, was this behavior ever correct with ln -s?

$ ls this-does-not-exist
ls: cannot access 'this-does-not-exist': No such file or directory
$ rm bad-link
$ ln -s this-does-not-exist bad-link
$ ls bad-link # it's highlighted link a broken link
bad-link

I think, long term, the correct logic is if (!resolvedSourceExists && !options.symlink). I wonder if this would be too big of a change (@freitagbr , what do you think?).

@loilo which bug does this address?

common.error('Source file does not exist', { continue: true });
} else if (isWindows && common.statFollowLinks(resolvedSourcePath).isDirectory()) {
linkType = 'junction';
Expand Down
21 changes: 21 additions & 0 deletions test/ln.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,15 @@ test('Inside existing directory', t => {
);
});

test('Link to dead source links', t => {
shell.cd(t.context.tmp);
const result = shell.ln('-s', 'badlink', 'link-to-dead-link');
t.is(result.code, 0);
t.falsy(result.stderr);
t.falsy(shell.error());
fs.lstatSync('link-to-dead-link');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we turn this into a real assertion? This would be a good use of common.isBrokenLink().

});

test('-f option', t => {
const result = shell.ln('-f', `${t.context.tmp}/file1.js`, `${t.context.tmp}/file2.js`);
t.is(result.code, 0);
Expand All @@ -181,6 +190,18 @@ test('-sf option', t => {
});
});

test('Override dead destination links with -sf', t => {
shell.cd(t.context.tmp);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optional: you could assert the precondition (that this link is in fact a dead link). I'd be fine with t.falsy(fs.existsSync('badlink')).

const result = shell.ln('-sf', 'file1.txt', 'badlink');
t.is(result.code, 0);
t.falsy(result.stderr);
t.falsy(shell.error());
t.is(
fs.readFileSync('file1.txt').toString(),
fs.readFileSync('badlink').toString()
);
});

test('Abspath regression', t => {
utils.skipOnWinForEPERM(shell.ln.bind(shell, '-sf', 'file1', path.resolve(`${t.context.tmp}/abspath`)), () => {
t.truthy(fs.existsSync(`${t.context.tmp}/abspath`));
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy