blob: cfa90f167d2c43b7e39954c65e80a8a1a0f93c11 [file] [log] [blame]
/**
* @license
* Copyright 2019 The Bazel Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as assert from 'node:assert'
import * as fs from 'node:fs'
import { withFixtures } from 'inline-fixtures'
import * as path from 'node:path'
import * as util from 'node:util'
import { patcher } from '../../node-patches/src/fs.cjs'
// We don't want to bring jest into this repo so we just fake the describe and it functions here
async function describe(_, fn) {
await fn()
}
async function it(_, fn) {
await fn()
}
describe('testing readlink', async () => {
await it('can resolve symlink in root', async () => {
await withFixtures(
{
a: {},
b: { file: 'contents' },
},
async (fixturesDir) => {
fixturesDir = fs.realpathSync(fixturesDir)
// create symlink from a to b
fs.symlinkSync(
path.join(fixturesDir, 'b', 'file'),
path.join(fixturesDir, 'a', 'link')
)
const revertPatches = patcher([path.join(fixturesDir)])
const linkPath = path.join(fixturesDir, 'a', 'link')
assert.deepStrictEqual(
fs.readlinkSync(linkPath),
path.join(fixturesDir, 'b', 'file'),
'SYNC: should read the symlink because its within root'
)
assert.deepStrictEqual(
fs.readlinkSync(new URL(`file://${linkPath}`)),
path.join(fixturesDir, 'b', 'file'),
'SYNC: should read the symlink because its within root'
)
assert.deepStrictEqual(
await util.promisify(fs.readlink)(
new URL(`file://${linkPath}`)
),
path.join(fixturesDir, 'b', 'file'),
'CB: should read the symlink because its within root'
)
assert.deepStrictEqual(
await util.promisify(fs.readlink)(linkPath),
path.join(fixturesDir, 'b', 'file'),
'CB: should read the symlink because its within root'
)
assert.deepStrictEqual(
await fs.promises.readlink(linkPath),
path.join(fixturesDir, 'b', 'file'),
'Promise: should read the symlink because its within root'
)
revertPatches()
}
)
})
await it("doesn't resolve as symlink outside of root", async () => {
await withFixtures(
{
a: {},
b: { file: 'contents' },
},
async (fixturesDir) => {
fixturesDir = fs.realpathSync(fixturesDir)
// create symlink from a to b
fs.symlinkSync(
path.join(fixturesDir, 'b', 'file'),
path.join(fixturesDir, 'a', 'link')
)
const revertPatches = patcher([path.join(fixturesDir, 'a')])
const linkPath = path.join(
fs.realpathSync(fixturesDir),
'a',
'link'
)
assert.throws(() => {
fs.readlinkSync(linkPath)
}, "should throw because it's not a link")
let thrown
try {
await util.promisify(fs.readlink)(linkPath)
} catch (e) {
thrown = e
} finally {
if (!thrown) assert.fail('must throw einval error')
}
thrown = undefined
try {
await fs.promises.readlink(linkPath)
} catch (e) {
thrown = e
} finally {
if (!thrown) assert.fail('must throw einval error')
}
revertPatches()
}
)
})
await it('can resolve symlink to a symlink in the sandbox if it has a corresponding location', async () => {
await withFixtures(
{
sandbox: {},
execroot: { file: 'contents' },
},
async (fixturesDir) => {
fixturesDir = fs.realpathSync(fixturesDir)
// create symlink from execroot/link to execroot/file
fs.symlinkSync(
path.join(fixturesDir, 'execroot', 'file'),
path.join(fixturesDir, 'execroot', 'link')
)
// create sandbox
fs.symlinkSync(
path.join(fixturesDir, 'execroot', 'file'),
path.join(fixturesDir, 'sandbox', 'file')
)
fs.symlinkSync(
path.join(fixturesDir, 'execroot', 'link'),
path.join(fixturesDir, 'sandbox', 'link')
)
const revertPatches = patcher([
path.join(fixturesDir, 'sandbox'),
])
const linkPath = path.join(fixturesDir, 'sandbox', 'link')
const filePath = path.join(fixturesDir, 'sandbox', 'file')
assert.deepStrictEqual(
fs.readlinkSync(linkPath),
filePath,
'SYNC: should read the symlink in the sandbox'
)
assert.deepStrictEqual(
fs.readlinkSync(new URL(`file://${linkPath}`)),
filePath,
'SYNC: should read the symlink in the sandbox'
)
assert.deepStrictEqual(
await util.promisify(fs.readlink)(
new URL(`file://${linkPath}`)
),
filePath,
'CB: should read the symlink in the sandbox'
)
assert.deepStrictEqual(
await util.promisify(fs.readlink)(linkPath),
filePath,
'CB: should read the symlink in the sandbox'
)
assert.deepStrictEqual(
await fs.promises.readlink(linkPath),
filePath,
'Promise: should read the symlink in the sandbox'
)
revertPatches()
}
)
})
await it('cant resolve symlink to a symlink in the sandbox if it has no corresponding location', async () => {
await withFixtures(
{
sandbox: {},
execroot: {},
otherroot: { file: 'contents' },
},
async (fixturesDir) => {
fixturesDir = fs.realpathSync(fixturesDir)
// create dangling symlink from execroot/link to execroot/file
fs.symlinkSync(
path.join(fixturesDir, 'otherroot', 'file'),
path.join(fixturesDir, 'execroot', 'link')
)
// create sandbox
fs.symlinkSync(
path.join(fixturesDir, 'execroot', 'link'),
path.join(fixturesDir, 'sandbox', 'link')
)
const revertPatches = patcher([
path.join(fixturesDir, 'sandbox'),
])
const linkPath = path.join(fixturesDir, 'sandbox', 'link')
const filePath = path.join(fixturesDir, 'sandbox', 'file')
assert.throws(() => {
fs.readlinkSync(linkPath)
}, "should throw because it's not a resolvable link")
let thrown
try {
await util.promisify(fs.readlink)(linkPath)
} catch (e) {
thrown = e
} finally {
if (!thrown) assert.fail('must throw einval error')
}
thrown = undefined
try {
await fs.promises.readlink(linkPath)
} catch (e) {
thrown = e
} finally {
if (!thrown) assert.fail('must throw einval error')
}
revertPatches()
}
)
})
await it('includes parent calls in stack traces', async function readlinkStackTest1() {
let err
try {
fs.readlinkSync(null)
} catch (e) {
err = e
} finally {
if (!err) assert.fail('readlinkSync should fail on invalid path')
if (!err.stack.includes('readlinkStackTest1'))
assert.fail(
`readlinkSync error stack should contain calling method: ${err.stack}`
)
}
})
})