From d5154a1ed9ec0ea8886cfd2366ed4fc419cc6099 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Thu, 25 Mar 2021 14:46:32 +0000 Subject: [PATCH] Some cleanup fixes for tests that makes sure temp directory isn't getting out of control. --- .../src/api/routes/tests/component.spec.js | 10 --------- .../src/tests/utilities/TestConfiguration.js | 2 ++ .../server/src/utilities/fileSystem/index.js | 21 +++++++++++++++++-- .../server/src/utilities/fileSystem/newApp.js | 8 ++++++- .../src/utilities/fileSystem/utilities.js | 6 +++--- 5 files changed, 31 insertions(+), 16 deletions(-) diff --git a/packages/server/src/api/routes/tests/component.spec.js b/packages/server/src/api/routes/tests/component.spec.js index a485939ae4..35394a1537 100644 --- a/packages/server/src/api/routes/tests/component.spec.js +++ b/packages/server/src/api/routes/tests/component.spec.js @@ -1,16 +1,6 @@ const { checkBuilderEndpoint } = require("./utilities/TestFunctions") const setup = require("./utilities") -jest.mock("../../../utilities/fileSystem/utilities", () => ({ - ...jest.requireActual("../../../utilities/fileSystem/utilities"), - retrieve: () => { - const { join } = require("path") - const library = join("@budibase", "standard-components") - const path = require.resolve(library).split(join("dist", "index.js"))[0] + "manifest.json" - return JSON.stringify(require(path)) - } -})) - describe("/component", () => { let request = setup.getRequest() let config = setup.getConfig() diff --git a/packages/server/src/tests/utilities/TestConfiguration.js b/packages/server/src/tests/utilities/TestConfiguration.js index a9723c8671..433cec4a0a 100644 --- a/packages/server/src/tests/utilities/TestConfiguration.js +++ b/packages/server/src/tests/utilities/TestConfiguration.js @@ -14,6 +14,7 @@ const { } = require("./structures") const controllers = require("./controllers") const supertest = require("supertest") +const { cleanup } = require("../../utilities/fileSystem") const EMAIL = "babs@babs.com" const PASSWORD = "babs_password" @@ -63,6 +64,7 @@ class TestConfiguration { if (this.server) { this.server.close() } + cleanup(this.allApps.map(app => app._id)) } defaultHeaders() { diff --git a/packages/server/src/utilities/fileSystem/index.js b/packages/server/src/utilities/fileSystem/index.js index 001bddd44c..971eaee5fc 100644 --- a/packages/server/src/utilities/fileSystem/index.js +++ b/packages/server/src/utilities/fileSystem/index.js @@ -157,11 +157,19 @@ exports.downloadTemplate = async (type, name) => { * Retrieves component libraries from object store (or tmp symlink if in local) */ exports.getComponentLibraryManifest = async (appId, library) => { - const devPath = join(budibaseTempDir(), library, "manifest.json") + const filename = "manifest.json" + /* istanbul ignore next */ + // when testing in cypress and so on we need to get the package + // as the environment may not be fully fleshed out for dev or prod + if (env.isTest()) { + const paths = await downloadLibraries(appId) + return require(join(paths[library], "package", filename)) + } + const devPath = join(budibaseTempDir(), library, filename) if (env.isDev() && fs.existsSync(devPath)) { return require(devPath) } - const path = join(appId, "node_modules", library, "package", "manifest.json") + const path = join(appId, "node_modules", library, "package", filename) let resp = await retrieve(ObjectStoreBuckets.APPS, path) if (typeof resp !== "string") { resp = resp.toString("utf8") @@ -201,6 +209,15 @@ exports.readFileSync = (filepath, options = "utf8") => { return fs.readFileSync(filepath, options) } +/** + * Given a set of app IDs makes sure file system is cleared of any of their temp info. + */ +exports.cleanup = appIds => { + for (let appId of appIds) { + fs.rmdirSync(join(budibaseTempDir(), appId), { recursive: true }) + } +} + /** * Full function definition for below can be found in the utilities. */ diff --git a/packages/server/src/utilities/fileSystem/newApp.js b/packages/server/src/utilities/fileSystem/newApp.js index ba3d13afed..2f5c77912a 100644 --- a/packages/server/src/utilities/fileSystem/newApp.js +++ b/packages/server/src/utilities/fileSystem/newApp.js @@ -11,13 +11,19 @@ const BUCKET_NAME = ObjectStoreBuckets.APPS exports.downloadLibraries = async appId => { const LIBRARIES = ["standard-components"] + const paths = {} // Need to download tarballs directly from NPM as our users may not have node on their machine for (let lib of LIBRARIES) { // download tarball const registryUrl = `https://registry.npmjs.org/@budibase/${lib}/-/${lib}-${packageJson.version}.tgz` const path = join(appId, "node_modules", "@budibase", lib) - await downloadTarball(registryUrl, BUCKET_NAME, path) + paths[`@budibase/${lib}`] = await downloadTarball( + registryUrl, + BUCKET_NAME, + path + ) } + return paths } exports.newAppPublicPath = async appId => { diff --git a/packages/server/src/utilities/fileSystem/utilities.js b/packages/server/src/utilities/fileSystem/utilities.js index 77e5160a5a..7b97790dfe 100644 --- a/packages/server/src/utilities/fileSystem/utilities.js +++ b/packages/server/src/utilities/fileSystem/utilities.js @@ -49,8 +49,6 @@ const PUBLIC_BUCKETS = [ObjectStoreBuckets.APPS] * @constructor */ exports.ObjectStore = bucket => { - console.log("CREATED OBJECT STORE") - console.trace() if (env.SELF_HOSTED) { AWS.config.update({ accessKeyId: env.MINIO_ACCESS_KEY, @@ -217,7 +215,9 @@ exports.downloadTarball = async (url, bucket, path) => { const tmpPath = join(budibaseTempDir(), path) await streamPipeline(response.body, zlib.Unzip(), tar.extract(tmpPath)) - await exports.uploadDirectory(bucket, tmpPath, path) + if (!env.isTest()) { + await exports.uploadDirectory(bucket, tmpPath, path) + } // return the temporary path incase there is a use for it return tmpPath }