chore: adjusts test snapshot logic to restore uploads properly

This commit is contained in:
James
2024-04-09 13:33:42 -04:00
parent 313ea52e3d
commit 9ad1cbe920
3 changed files with 19 additions and 18 deletions

View File

@@ -67,10 +67,10 @@ export async function seedDB({
/**
* Restore uploads dir if it exists
*/
if (uploadsDir && fs.existsSync(uploadsDirCache.path)) {
if (uploadsDir && fs.existsSync(uploadsDirCache[snapshotKey])) {
// move all files from inside uploadsDirCacheFolder to uploadsDir
await fs.promises
.readdir(uploadsDirCache.path, { withFileTypes: true })
.readdir(uploadsDirCache[snapshotKey], { withFileTypes: true })
.then(async (files) => {
for (const file of files) {
if (file.isDirectory()) {
@@ -78,12 +78,12 @@ export async function seedDB({
recursive: true,
})
await fs.promises.copyFile(
path.join(uploadsDirCache.path, file.name),
path.join(uploadsDirCache[snapshotKey], file.name),
path.join(uploadsDir, file.name),
)
} else {
await fs.promises.copyFile(
path.join(uploadsDirCache.path, file.name),
path.join(uploadsDirCache[snapshotKey], file.name),
path.join(uploadsDir, file.name),
)
}
@@ -106,7 +106,6 @@ export async function seedDB({
if (isMongoose(_payload)) {
await Promise.all([
...collectionSlugs.map(async (collectionSlug) => {
// @ts-expect-error TODO: Type this better
await _payload.db.collections[collectionSlug].createIndexes()
}),
])
@@ -134,33 +133,37 @@ export async function seedDB({
* Cache uploads dir to a cache folder if uploadsDir exists
*/
if (!alwaysSeed && uploadsDir && fs.existsSync(uploadsDir)) {
if (!uploadsDirCache.path) {
if (!uploadsDirCache[snapshotKey]) {
// Define new cache folder path to the OS temp directory (well a random folder inside it)
uploadsDirCache.path = path.join(os.tmpdir(), `payload-e2e-tests-uploads-cache`)
uploadsDirCache[snapshotKey] = path.join(
os.tmpdir(),
`${snapshotKey}`,
`payload-e2e-tests-uploads-cache`,
)
}
// delete the cache folder if it exists
if (fs.existsSync(uploadsDirCache.path)) {
await fs.promises.rm(uploadsDirCache.path, { recursive: true })
if (fs.existsSync(uploadsDirCache[snapshotKey])) {
await fs.promises.rm(uploadsDirCache[snapshotKey], { recursive: true })
}
await fs.promises.mkdir(uploadsDirCache.path, { recursive: true })
await fs.promises.mkdir(uploadsDirCache[snapshotKey], { recursive: true })
// recursively move all files and directories from uploadsDir to uploadsDirCacheFolder
await fs.promises
.readdir(uploadsDir, { withFileTypes: true })
.then(async (files) => {
for (const file of files) {
if (file.isDirectory()) {
await fs.promises.mkdir(path.join(uploadsDirCache.path, file.name), {
await fs.promises.mkdir(path.join(uploadsDirCache[snapshotKey], file.name), {
recursive: true,
})
await fs.promises.copyFile(
path.join(uploadsDir, file.name),
path.join(uploadsDirCache.path, file.name),
path.join(uploadsDirCache[snapshotKey], file.name),
)
} else {
await fs.promises.copyFile(
path.join(uploadsDir, file.name),
path.join(uploadsDirCache.path, file.name),
path.join(uploadsDirCache[snapshotKey], file.name),
)
}
}

View File

@@ -7,10 +7,8 @@ import { sql } from 'drizzle-orm'
import { isMongoose } from './isMongoose.js'
export const uploadsDirCache: {
path: null | string
} = {
path: null,
}
[key: string]: string
} = {}
export const dbSnapshot = {}
async function createMongooseSnapshot(collectionsObj, snapshotKey: string) {

View File

@@ -161,4 +161,4 @@
".next/types/**/*.ts",
"scripts/**/*.ts"
]
}
}