|
| 1 | +import { join } from 'path' |
| 2 | +import process from 'process' |
| 3 | + |
| 4 | +import Chance from 'chance' |
| 5 | +import { copy, existsSync } from 'fs-extra' |
| 6 | +import { dir as getTmpDir } from 'tmp-promise' |
| 7 | + |
| 8 | +import { setupImageCdn } from '../../../src/helpers/functions' |
| 9 | + |
| 10 | +const SAMPLE_PROJECT_DIR = `${__dirname}/../../../../demo` |
| 11 | +const TEST_TIMEOUT = 60_000 |
| 12 | + |
| 13 | +const changeCwd = (cwd) => { |
| 14 | + const originalCwd = process.cwd() |
| 15 | + process.chdir(cwd) |
| 16 | + return () => { |
| 17 | + process.chdir(originalCwd) |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +// Move gatsby project from sample project to current directory |
| 22 | +const moveGatsbyDir = async () => { |
| 23 | + await copy(SAMPLE_PROJECT_DIR, join(process.cwd())) |
| 24 | +} |
| 25 | + |
| 26 | +const chance = new Chance() |
| 27 | + |
| 28 | +describe('setupImageCdn', () => { |
| 29 | + let cleanup |
| 30 | + let restoreCwd |
| 31 | + |
| 32 | + beforeEach(async () => { |
| 33 | + const tmpDir = await getTmpDir({ unsafeCleanup: true }) |
| 34 | + |
| 35 | + restoreCwd = changeCwd(tmpDir.path) |
| 36 | + // eslint-disable-next-line prefer-destructuring |
| 37 | + cleanup = tmpDir.cleanup |
| 38 | + }) |
| 39 | + |
| 40 | + afterEach(async () => { |
| 41 | + // Cleans up the temporary directory from `getTmpDir()` and do not make it |
| 42 | + // the current directory anymore |
| 43 | + restoreCwd() |
| 44 | + await cleanup() |
| 45 | + }, TEST_TIMEOUT) |
| 46 | + it( |
| 47 | + 'successfully creates a functions-internal directory if it does not exist', |
| 48 | + async () => { |
| 49 | + await moveGatsbyDir() |
| 50 | + const constants = { |
| 51 | + INTERNAL_FUNCTIONS_SRC: 'demo/.netlify/functions-internal', |
| 52 | + PUBLISH_DIR: 'demo/public', |
| 53 | + FUNCTIONS_DIST: 'demo/.netlify/functions', |
| 54 | + EDGE_HANDLERS_DIST: 'demo/.netlify/edge-functions-dist/', |
| 55 | + IS_LOCAL: true, |
| 56 | + NETLIFY_BUILD_VERSION: '9000.0.0', |
| 57 | + SITE_ID: chance.guid(), |
| 58 | + } |
| 59 | + |
| 60 | + const netlifyConfig = { |
| 61 | + build: { |
| 62 | + command: 'npm run build', |
| 63 | + publish: 'demo/public', |
| 64 | + base: '.', |
| 65 | + environment: { |
| 66 | + GATSBY_CLOUD_IMAGE_CDN: 'true', |
| 67 | + }, |
| 68 | + services: {}, |
| 69 | + processing: { |
| 70 | + css: {}, |
| 71 | + js: {}, |
| 72 | + html: {}, |
| 73 | + images: {}, |
| 74 | + }, |
| 75 | + }, |
| 76 | + functions: { '*': {} }, |
| 77 | + redirects: [], |
| 78 | + headers: [], |
| 79 | + edge_handlers: [], |
| 80 | + plugins: [], |
| 81 | + } |
| 82 | + |
| 83 | + await setupImageCdn({ constants, netlifyConfig }) |
| 84 | + |
| 85 | + const doesDirectoryExist = existsSync( |
| 86 | + join(process.cwd(), 'demo', '.netlify', 'functions-internal'), |
| 87 | + ) |
| 88 | + |
| 89 | + expect(doesDirectoryExist).toEqual(true) |
| 90 | + // Longer timeout for the test is necessary due to the copying of the demo project into the tmp dir |
| 91 | + }, |
| 92 | + TEST_TIMEOUT, |
| 93 | + ) |
| 94 | +}) |
0 commit comments