-
Notifications
You must be signed in to change notification settings - Fork 269
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
wp-now: Add tests that check resulting fs in different modes #339
Changes from 13 commits
8a63396
5a125e8
850504b
a49ff4b
44ed14c
b362d84
8c827fd
2dd1a9c
acece29
33d8444
bf814eb
12d9da3
c189b4b
cc42e91
7619838
ab8eb33
5983b8b
68b855c
f57b84c
ee3af99
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
import { inferMode, parseOptions, WPNowMode, WPNowOptions } from '../wp-now'; | ||
import startWPNow, { | ||
inferMode, | ||
parseOptions, | ||
WPNowMode, | ||
WPNowOptions, | ||
} from '../wp-now'; | ||
import fs from 'fs-extra'; | ||
import path from 'path'; | ||
import { | ||
|
@@ -8,7 +13,8 @@ import { | |
isWordPressDirectory, | ||
isWordPressDevelopDirectory, | ||
} from '../wp-playground-wordpress'; | ||
import jest from 'jest-mock'; | ||
import os from 'os'; | ||
import crypto from 'crypto'; | ||
|
||
const exampleDir = __dirname + '/mode-examples'; | ||
|
||
|
@@ -144,3 +150,143 @@ test('isWordPressDevelopDirectory returns false for incomplete WordPress-develop | |
expect(isWordPressDevelopDirectory(projectPath)).toBe(false); | ||
expect(inferMode(projectPath)).toBe(WPNowMode.INDEX); | ||
}); | ||
|
||
describe('Test starting different modes', () => { | ||
let tmpExampleDirectory; | ||
|
||
/** | ||
* Copy example directory to a temporary directory | ||
*/ | ||
beforeEach(() => { | ||
const tmpDirectory = os.tmpdir(); | ||
const directoryHash = crypto.randomBytes(20).toString('hex'); | ||
|
||
tmpExampleDirectory = path.join( | ||
tmpDirectory, | ||
`wp-now-tests-${directoryHash}` | ||
); | ||
fs.ensureDirSync(tmpExampleDirectory); | ||
fs.copySync(exampleDir, tmpExampleDirectory); | ||
}); | ||
|
||
/** | ||
* Remove temporary directory | ||
*/ | ||
afterEach(() => { | ||
fs.rmSync(tmpExampleDirectory, { recursive: true, force: true }); | ||
}); | ||
|
||
/** | ||
* Expect that all provided mount point paths are empty directories which are result of file system mounts. | ||
* | ||
* @param mountPaths List of mount point paths that should exist on file system. | ||
* @param projectPath Project path. | ||
*/ | ||
const expectEmptyMountPoints = (mountPaths, projectPath) => { | ||
mountPaths.map((relativePath) => { | ||
const fullPath = path.join(projectPath, relativePath); | ||
|
||
expect(fs.existsSync(fullPath)).toBe(true); | ||
expect(fs.readdirSync(fullPath)).toEqual([]); | ||
expect(fs.lstatSync(fullPath).isDirectory()).toBe(true); | ||
}); | ||
}; | ||
|
||
/** | ||
* Expect that all required files exist for PHP. | ||
* | ||
* @param requiredFiles List of files that should be accessible by PHP. | ||
* @param documentRoot Document root of the PHP server. | ||
* @param php NodePHP instance. | ||
*/ | ||
const expectRequiredFiles = (requiredFiles, documentRoot, php) => { | ||
requiredFiles.map((relativePath) => { | ||
const fullPath = path.join(documentRoot, relativePath); | ||
expect(php.fileExists(fullPath)).toBe(true); | ||
}); | ||
}; | ||
|
||
/** | ||
* Test that startWPNow in "index", "plugin" and "theme" modes doesn't change anything in the project directory. | ||
*/ | ||
test.each(['index', 'plugin', 'theme'])( | ||
'startWPNow starts %s mode', | ||
async (mode) => { | ||
const exampleProjectPath = path.join(exampleDir, mode); | ||
const projectPath = path.join(tmpExampleDirectory, mode); | ||
|
||
const rawOptions: Partial<WPNowOptions> = { | ||
projectPath: projectPath, | ||
}; | ||
|
||
await startWPNow(rawOptions); | ||
|
||
expect(fs.readdirSync(projectPath)).toEqual( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it'd be better to have explicit assertions here, instead of reading data and assigning it to a variable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I already changed it - I'm not assigning those to variables but comparing the content of two paths. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, but it's still variable data. I think it should be static, otherwise it may break unexpectedly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @danielbachhuber so would you use an array of paths for each There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @wojtekn Correct. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
fs.readdirSync(exampleProjectPath) | ||
); | ||
} | ||
); | ||
|
||
/** | ||
* Test that startWPNow in "wp-content" mode mounts required files and directories, and | ||
* that required files exist for PHP. | ||
*/ | ||
test('startWPNow starts wp-content mode', async () => { | ||
const projectPath = path.join(tmpExampleDirectory, 'wp-content'); | ||
|
||
const rawOptions: Partial<WPNowOptions> = { | ||
projectPath: projectPath, | ||
}; | ||
|
||
const { php, options: wpNowOptions } = await startWPNow(rawOptions); | ||
|
||
const mountPointPaths = [ | ||
'database', | ||
'db.php', | ||
'mu-plugins', | ||
'plugins/sqlite-database-integration', | ||
]; | ||
|
||
expectEmptyMountPoints(mountPointPaths, projectPath); | ||
|
||
const requiredFiles = [ | ||
'wp-content/db.php', | ||
'wp-content/mu-plugins/0-allow-wp-org.php', | ||
'playground-consts.json', | ||
]; | ||
|
||
expectRequiredFiles(requiredFiles, wpNowOptions.documentRoot, php); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are there any files we don't expect to be placed in the project directory? Can we add tests for them? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, as an example, that |
||
}); | ||
|
||
/** | ||
* Test that startWPNow in "wordpress" mode mounts required files and directories, and | ||
* that required files exist for PHP. | ||
*/ | ||
test('startWPNow starts wordpress mode', async () => { | ||
const projectPath = path.join(tmpExampleDirectory, 'wordpress'); | ||
|
||
const rawOptions: Partial<WPNowOptions> = { | ||
projectPath: projectPath, | ||
}; | ||
|
||
const { php, options: wpNowOptions } = await startWPNow(rawOptions); | ||
|
||
const mountPointPaths = [ | ||
'wp-content/database', | ||
'wp-content/db.php', | ||
'wp-content/mu-plugins', | ||
'wp-content/plugins/sqlite-database-integration', | ||
]; | ||
|
||
expectEmptyMountPoints(mountPointPaths, projectPath); | ||
|
||
const requiredFiles = [ | ||
'wp-content/db.php', | ||
'wp-content/mu-plugins/0-allow-wp-org.php', | ||
'playground-consts.json', | ||
'wp-config.php', | ||
]; | ||
|
||
expectRequiredFiles(requiredFiles, wpNowOptions.documentRoot, php); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add a
wp-now-tests-
prefix to thedirectoryHash
so it's somewhat obvious where these directories are coming from.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, I've added the flag. Note that we are removing the directory in
afterEach
, so ideally they shouldn't stay in/tmp
after tests are executed. It is still a good change for cases in which the test crashes and doesn't run the cleanup method.