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

removed fake file system usage, update fs-extra to v11 #656

Merged
merged 1 commit into from
Apr 19, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions jsconfig.json

This file was deleted.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"dedent-js": "^1.0.1",
"dotenv": "^16",
"execa": "^5.0.0",
"fs-extra": "^10",
"fs-extra": "^11.1.1",
"get-port": "^5",
"hjson": "^3.2.1",
"http-terminator": "^3",
Expand Down Expand Up @@ -61,7 +61,6 @@
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^6.0.0",
"jest": "^29.5.0",
"jest-plugin-fs": "^2.9.0",
"nock": "^13.2.9",
"oclif": "^3.2.0",
"stdout-stderr": "^0.1.9"
Expand Down
1 change: 0 additions & 1 deletion test/commands/lib/log-poller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTA
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
global.mockFs()
const { EventPoller, run: logPoller } = require('../../../src/lib/log-poller')
const { printActionLogs } = require('@adobe/aio-lib-runtime')
const mockLogger = require('@adobe/aio-lib-core-logging')
Expand Down
179 changes: 86 additions & 93 deletions test/commands/lib/vscode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,11 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

// enable the fake filesystem mocks
global.mockFs()

const vscode = require('../../../src/lib/vscode')
const yeoman = require('yeoman-environment')
const fs = require('fs-extra')
const path = require('path')
const dataMocks = require('../../data-mocks/config-loader')
const upath = require('upath')

jest.mock('fs-extra')
jest.mock('yeoman-environment')
Expand All @@ -36,22 +32,38 @@ const createAppConfig = (aioConfig = {}, appFixtureName = 'legacy-app') => {
return appConfig
}

const relativeVsCodeConfigFiles = (configRoot, vsCodeConfig) => {
const { backupFile, mainFile } = vsCodeConfig.files()
return {
backupFile: upath.toUnix(path.join('/', path.relative(configRoot, backupFile))),
mainFile: upath.toUnix(path.join('/', path.relative(configRoot, mainFile)))
const createFileSystem = (initialFiles = {}) => {
const myFileSystem = { ...initialFiles }

fs.existsSync.mockImplementation((filePath) => {
return (!!myFileSystem[filePath])
})

fs.moveSync.mockImplementation((src, dest) => {
myFileSystem[dest] = myFileSystem[src]
delete myFileSystem[src]
})

const removeFile = (filePath) => {
delete myFileSystem[filePath]
}

fs.unlinkSync.mockImplementation(removeFile)
fs.rmdirSync.mockImplementation(removeFile)

fs.readdirSync.mockImplementation((filePath) => {
const item = myFileSystem[filePath]
if (!Array.isArray(item)) {
throw new Error(`Fake filesystem ${filePath} value does not contain an array.`)
}
return item
})

return myFileSystem
}

beforeEach(() => {
global.fakeFileSystem.reset()
mockYeomanInstantiate.mockClear()
mockYeomanRunGenerator.mockImplementation(() => {
global.fakeFileSystem.addJson({
'.vscode/launch.json': 'generated-content'
})
})
})

test('exports', () => {
Expand All @@ -76,181 +88,162 @@ describe('update()', () => {
const props = { frontEndUrl: 'https://foo.bar' }
const config = { ...createAppConfig().application, envFile: 'my.env' }
const vsCodeConfig = vscode(config)
const { backupFile, mainFile } = relativeVsCodeConfigFiles(config.root, vsCodeConfig)
const { backupFile, mainFile } = vsCodeConfig.files()

const mockYeomanOutput = (fileSystem) => {
mockYeomanRunGenerator.mockImplementation(() => {
fileSystem[mainFile] = 'generated-content'
})
}

test('launch.json does not exist, backup does not exist (no backup copy)', async () => {
let globalFs = global.fakeFileSystem.files()
const myFileSystem = createFileSystem()
mockYeomanOutput(myFileSystem)

// launch.json does not exist
expect(mainFile in globalFs).toEqual(false)
expect(mainFile in myFileSystem).toEqual(false)
// backup does not exist
expect(backupFile in globalFs).toEqual(false)
expect(backupFile in myFileSystem).toEqual(false)

await vsCodeConfig.update(props)

globalFs = global.fakeFileSystem.files()

// now launch.json exists
expect(mainFile in globalFs).toEqual(true)
expect(globalFs[mainFile].toString()).toEqual('generated-content')
expect(mainFile in myFileSystem).toEqual(true)
expect(myFileSystem[mainFile].toString()).toEqual('generated-content')
// backup should not exist
expect(backupFile in globalFs).toEqual(false)
expect(backupFile in myFileSystem).toEqual(false)
})

test('launch.json exists, backup does not exist (copy to backup)', async () => {
let globalFs

global.fakeFileSystem.addJson({
const myFileSystem = createFileSystem({
[mainFile]: 'main-content'
})

globalFs = global.fakeFileSystem.files()
mockYeomanOutput(myFileSystem)

// launch.json already exists
expect(mainFile in globalFs).toEqual(true)
expect(mainFile in myFileSystem).toEqual(true)
// backup does not exist
expect(backupFile in globalFs).toEqual(false)
expect(backupFile in myFileSystem).toEqual(false)

await vsCodeConfig.update(props)

globalFs = global.fakeFileSystem.files()
expect(fs.existsSync).toHaveBeenCalled()
expect(fs.moveSync).toHaveBeenCalled()

// still exists, but is generated
expect(mainFile in globalFs).toEqual(true)
expect(globalFs[mainFile].toString()).toEqual('generated-content')
expect(mainFile in myFileSystem).toEqual(true)
expect(myFileSystem[mainFile].toString()).toEqual('generated-content')
// check backup is copied
expect(backupFile in globalFs).toEqual(true)
expect(globalFs[backupFile].toString()).toEqual('main-content')
expect(backupFile in myFileSystem).toEqual(true)
expect(myFileSystem[backupFile].toString()).toEqual('main-content')
})

test('launch.json exists, backup exists (do not overwrite backup)', async () => {
let globalFs

global.fakeFileSystem.addJson({
const myFileSystem = createFileSystem({
[mainFile]: 'main-content',
[backupFile]: 'backup-content'
})

globalFs = global.fakeFileSystem.files()
mockYeomanOutput(myFileSystem)

// launch.json already exists
expect(mainFile in globalFs).toEqual(true)
expect(mainFile in myFileSystem).toEqual(true)
// backup already exists
expect(backupFile in globalFs).toEqual(true)
expect(backupFile in myFileSystem).toEqual(true)

await vsCodeConfig.update(props)

globalFs = global.fakeFileSystem.files()

// still exists, but is generated
expect(mainFile in globalFs).toEqual(true)
expect(globalFs[mainFile].toString()).toEqual('generated-content')
expect(mainFile in myFileSystem).toEqual(true)
expect(myFileSystem[mainFile].toString()).toEqual('generated-content')
// check backup is *not* copied over
expect(backupFile in globalFs).toEqual(true)
expect(globalFs[backupFile].toString()).toEqual('backup-content')
expect(backupFile in myFileSystem).toEqual(true)
expect(myFileSystem[backupFile].toString()).toEqual('backup-content')
})
})

describe('cleanup()', () => {
const config = { ...createAppConfig().application, envFile: 'my.env' }
const vsCodeConfig = vscode(config)
const { backupFile, mainFile } = relativeVsCodeConfigFiles(config.root, vsCodeConfig)
const { backupFile, mainFile } = vsCodeConfig.files()

test('launch.json does not exist, backup does not exist (do nothing)', () => {
let globalFs = global.fakeFileSystem.files()
const myFileSystem = createFileSystem()

// launch.json does not exist
expect(mainFile in globalFs).toEqual(false)
expect(mainFile in myFileSystem).toEqual(false)
// backup already exists
expect(backupFile in globalFs).toEqual(false)
expect(backupFile in myFileSystem).toEqual(false)

vsCodeConfig.cleanup()

globalFs = global.fakeFileSystem.files()

// nothing should exist still
expect(mainFile in globalFs).toEqual(false)
expect(backupFile in globalFs).toEqual(false)
expect(mainFile in myFileSystem).toEqual(false)
expect(backupFile in myFileSystem).toEqual(false)
})

test('launch.json exists, backup does not exist (remove launch.json, .vscode folder empty and is deleted)', () => {
let globalFs
const vscodeFolder = path.dirname(mainFile)

global.fakeFileSystem.addJson({
[mainFile]: 'main-content'
const myFileSystem = createFileSystem({
[mainFile]: 'main-content',
[vscodeFolder]: [] // nothing in it
})

globalFs = global.fakeFileSystem.files()

// launch.json exists
expect(mainFile in globalFs).toEqual(true)
expect(mainFile in myFileSystem).toEqual(true)
// backup does not exist
expect(backupFile in globalFs).toEqual(false)
expect(backupFile in myFileSystem).toEqual(false)

vsCodeConfig.cleanup()

globalFs = global.fakeFileSystem.files()

// launch.json and the backup file should not exist
expect(mainFile in globalFs).toEqual(false)
expect(backupFile in globalFs).toEqual(false)
expect(mainFile in myFileSystem).toEqual(false)
expect(backupFile in myFileSystem).toEqual(false)
// the .vscode folder should be deleted as well (since there are no other contents)
expect(fs.existsSync(vscodeFolder)).toBe(false)
})

test('launch.json exists, backup does not exist (remove launch.json, .vscode folder not empty and is not deleted)', () => {
let globalFs
const vscodeFolder = path.dirname(mainFile)
const someOtherFileInVsCodeFolder = upath.toUnix(path.join(vscodeFolder, 'some-file'))
const someOtherFileInVsCodeFolder = path.join(vscodeFolder, 'some-file')

global.fakeFileSystem.addJson({
const myFileSystem = createFileSystem({
[mainFile]: 'main-content',
[vscodeFolder]: [someOtherFileInVsCodeFolder],
[someOtherFileInVsCodeFolder]: 'some-content'
})

globalFs = global.fakeFileSystem.files()

// launch.json exists
expect(mainFile in globalFs).toEqual(true)
expect(mainFile in myFileSystem).toEqual(true)
// backup does not exist
expect(backupFile in globalFs).toEqual(false)
expect(backupFile in myFileSystem).toEqual(false)

vsCodeConfig.cleanup()

globalFs = global.fakeFileSystem.files()

// launch.json and the backup file should not exist
expect(mainFile in globalFs).toEqual(false)
expect(backupFile in globalFs).toEqual(false)
expect(mainFile in myFileSystem).toEqual(false)
expect(backupFile in myFileSystem).toEqual(false)
// if there are any other content in .vscode, it is not deleted
expect(fs.existsSync(vscodeFolder)).toBe(true)
expect(someOtherFileInVsCodeFolder in globalFs).toEqual(true)
expect(someOtherFileInVsCodeFolder in myFileSystem).toEqual(true)
})

test('launch.json exists, backup exists (restore backup)', () => {
let globalFs

global.fakeFileSystem.addJson({
const myFileSystem = createFileSystem({
[mainFile]: 'main-content',
[backupFile]: 'backup-content'
})

globalFs = global.fakeFileSystem.files()

// launch.json exists
expect(mainFile in globalFs).toEqual(true)
expect(mainFile in myFileSystem).toEqual(true)
// backup exists
expect(backupFile in globalFs).toEqual(true)
expect(backupFile in myFileSystem).toEqual(true)

vsCodeConfig.cleanup()

globalFs = global.fakeFileSystem.files()

// launch.json restored from backup
expect(mainFile in globalFs).toEqual(true)
expect(globalFs[mainFile].toString()).toEqual('backup-content')
expect(mainFile in myFileSystem).toEqual(true)
expect(myFileSystem[mainFile].toString()).toEqual('backup-content')
// backup should not exist
expect(backupFile in globalFs).toEqual(false)
expect(backupFile in myFileSystem).toEqual(false)
})
})
45 changes: 0 additions & 45 deletions test/jest.setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,51 +20,6 @@ const eol = require('eol')
const path = require('path')
const hjson = require('hjson')

const fileSystem = require('jest-plugin-fs').default

// dont touch the real fs
global.mockFs = () => {
jest.unmock('fs-extra')
jest.mock('fs', () => require('jest-plugin-fs/mock'))

// set the fake filesystem
global.fakeFileSystem = {
addJson: (json) => {
// add to existing
fileSystem.mock(json)
},
removeKeys: (arr) => {
// remove from existing
const files = fileSystem.files()
for (const prop in files) {
if (arr.includes(prop)) {
delete files[prop]
}
}
fileSystem.restore()
fileSystem.mock(files)
},
clear: () => {
// reset to empty
fileSystem.restore()
},
reset: () => {
// reset file system
// TODO: add any defaults
fileSystem.restore()
},
files: () => {
return fileSystem.files()
}
}
// seed the fake filesystem
global.fakeFileSystem.reset()
}

global.unmockFs = () => {

}

// trap console log
beforeEach(() => {
stdout.start()
Expand Down