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

feat: Allow explicit null encoding for readFile/writeFile #18534

Merged
merged 16 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 31 additions & 0 deletions packages/driver/cypress/integration/commands/files_spec.js
Expand Up @@ -37,6 +37,21 @@ describe('src/cy/commands/files', () => {
})
})

it('passes explicit null encoding through to server and decodes response', () => {
BlueWinds marked this conversation as resolved.
Show resolved Hide resolved
Cypress.backend.resolves({
contents: Buffer.from('\n').toString('base64'),
filePath: '/path/to/foo.json',
})

cy.readFile('foo.json', null).then(() => {
expect(Cypress.backend).to.be.calledWith(
'read:file',
'foo.json',
{ encoding: null },
)
}).should('eql', Buffer.from('\n'))
})

it('sets the contents as the subject', () => {
Cypress.backend.resolves(okResponse)

Expand Down Expand Up @@ -340,6 +355,22 @@ describe('src/cy/commands/files', () => {
})
})

it('explicit null encoding is sent to server as base64 string', () => {
Cypress.backend.resolves(okResponse)

cy.writeFile('foo.txt', Buffer.from([0, 0, 54, 255]), null).then(() => {
expect(Cypress.backend).to.be.calledWith(
'write:file',
'foo.txt',
'AAA2/w==',
{
encoding: 'base64',
flag: 'w',
},
)
})
})

it('can take encoding as part of options', () => {
Cypress.backend.resolves(okResponse)

Expand Down
17 changes: 13 additions & 4 deletions packages/driver/src/cy/commands/files.ts
Expand Up @@ -11,11 +11,11 @@ export default (Commands, Cypress, cy) => {

if (_.isObject(encoding)) {
userOptions = encoding
encoding = null
encoding = undefined
BlueWinds marked this conversation as resolved.
Show resolved Hide resolved
}

options = _.defaults({}, userOptions, {
encoding: encoding != null ? encoding : 'utf8',
encoding: encoding === undefined ? 'utf8' : encoding,
flotwig marked this conversation as resolved.
Show resolved Hide resolved
log: true,
})

Expand Down Expand Up @@ -53,6 +53,10 @@ export default (Commands, Cypress, cy) => {
args: { cmd: 'readFile', action: 'read', file, filePath: err.filePath, error: err.message },
})
}).then(({ contents, filePath }) => {
if (options.encoding === null) {
contents = Buffer.from(contents, 'base64')
}

consoleProps['File Path'] = filePath
consoleProps['Contents'] = contents

Expand Down Expand Up @@ -85,11 +89,11 @@ export default (Commands, Cypress, cy) => {

if (_.isObject(encoding)) {
userOptions = encoding
encoding = null
encoding = undefined
}

options = _.defaults({}, userOptions, {
encoding: encoding ? encoding : 'utf8',
encoding: encoding === undefined ? 'utf8' : encoding,
flag: userOptions.flag ? userOptions.flag : 'w',
log: true,
})
Expand Down Expand Up @@ -120,6 +124,11 @@ export default (Commands, Cypress, cy) => {
})
}

if (Buffer.isBuffer(contents)) {
contents = contents.toString('base64')
options.encoding = 'base64'
}

if (_.isObject(contents)) {
contents = JSON.stringify(contents, null, 2)
}
Expand Down
6 changes: 3 additions & 3 deletions packages/server/lib/files.js
Expand Up @@ -6,10 +6,10 @@ module.exports = {
const filePath = path.resolve(projectRoot, file)
const readFn = path.extname(filePath) === '.json' ? fs.readJsonAsync : fs.readFileAsync

return readFn(filePath, options.encoding || 'utf8')
return readFn(filePath, options.encoding === undefined ? 'utf8' : options.encoding)
.then((contents) => {
return {
contents,
contents: options.encoding === null ? contents.toString('base64') : contents,
filePath,
}
})
Expand All @@ -22,7 +22,7 @@ module.exports = {
writeFile (projectRoot, file, contents, options = {}) {
const filePath = path.resolve(projectRoot, file)
const writeOptions = {
encoding: options.encoding || 'utf8',
encoding: options.encoding === undefined ? 'utf8' : options.encoding,
flag: options.flag || 'w',
}

Expand Down
14 changes: 14 additions & 0 deletions packages/server/test/unit/files_spec.js
Expand Up @@ -41,6 +41,12 @@ describe('lib/files', () => {
})
})

it('explicit null encoding is sent to driver as base64 string', function () {
return files.readFile(this.projectRoot, 'tests/_fixtures/ascii.foo', { encoding: null }).then(({ contents }) => {
expect(contents).to.eql(Buffer.from('\n').toString('base64'))
})
})

it('parses json to valid JS object', function () {
return files.readFile(this.projectRoot, 'tests/_fixtures/users.json').then(({ contents }) => {
expect(contents).to.eql([
Expand Down Expand Up @@ -75,6 +81,14 @@ describe('lib/files', () => {
})
})

it('explicit null encoding is read from driver as base64 string', function () {
return files.writeFile(this.projectRoot, '.projects/write_file.txt', Buffer.from(''), { encoding: null }).then(() => {
return files.readFile(this.projectRoot, '.projects/write_file.txt', { encoding: null }).then(({ contents }) => {
expect(contents).to.eql(Buffer.from('').toString('base64'))
})
})
})

it('overwrites existing file by default', function () {
return files.writeFile(this.projectRoot, '.projects/write_file.txt', 'foo').then(() => {
return files.readFile(this.projectRoot, '.projects/write_file.txt').then(({ contents }) => {
Expand Down