|
| 1 | +import { beforeEach, describe, expect, it } from 'vitest'; |
| 2 | +import { PluginTester } from 'codify-plugin-test'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { ResourceOperation } from 'codify-schemas'; |
| 5 | +import fs from 'node:fs'; |
| 6 | +import os from 'node:os'; |
| 7 | + |
| 8 | +describe('File resource tests', () => { |
| 9 | + let plugin: PluginTester; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + plugin = new PluginTester(path.resolve('./src/index.ts')); |
| 13 | + }) |
| 14 | + |
| 15 | + it('Can create a file, modify the contents and delete it', { timeout: 300000 }, async () => { |
| 16 | + const contents = 'AWS_ACCESS_KEY_ID=\n' + |
| 17 | + 'AWS_SECRET_ACCESS_KEY=\n' + |
| 18 | + 'AWS_S3_ENDPOINT=\n' + |
| 19 | + 'AWS_REGION=\n'; |
| 20 | + |
| 21 | + await plugin.fullTest([ |
| 22 | + { type: 'file', |
| 23 | + path: '~/.env', |
| 24 | + contents |
| 25 | + } |
| 26 | + ], { |
| 27 | + validateApply: (plans) => { |
| 28 | + expect(plans[0]).toMatchObject({ |
| 29 | + operation: ResourceOperation.CREATE, |
| 30 | + }) |
| 31 | + |
| 32 | + expect(fs.readFileSync(path.resolve(os.homedir(), '.env'), 'utf-8')).to.eq(contents); |
| 33 | + }, |
| 34 | + testModify: { |
| 35 | + modifiedConfigs: [{ |
| 36 | + type: 'file', |
| 37 | + path: '~/.env', |
| 38 | + contents: 'testing\ntest', |
| 39 | + }], |
| 40 | + validateModify: (plans) => { |
| 41 | + expect(plans[0]).toMatchObject({ |
| 42 | + operation: ResourceOperation.MODIFY, |
| 43 | + }) |
| 44 | + expect(fs.readFileSync(path.resolve(os.homedir(), '.env'), 'utf-8')).to.eq('testing\ntest'); |
| 45 | + } |
| 46 | + }, |
| 47 | + validateDestroy: () => { |
| 48 | + expect(fs.existsSync(path.resolve(os.homedir(), '.env'))).to.be.false; |
| 49 | + } |
| 50 | + }) |
| 51 | + }) |
| 52 | + |
| 53 | + it('Will throw an error if the path given is a directory', { timeout: 300000 }, async () => { |
| 54 | + fs.mkdirSync(path.resolve(os.homedir(), 'tmp')) |
| 55 | + |
| 56 | + await expect(async () => plugin.fullTest([ |
| 57 | + { type: 'file', path: '~/tmp', contents: 'anything' } |
| 58 | + ])).rejects.toThrow(); |
| 59 | + }) |
| 60 | + |
| 61 | + it('Will not modify a file if already created and onlyCreate is set to true', { timeout: 300000 }, async () => { |
| 62 | + const filePath = path.resolve(os.homedir(), 'testFile'); |
| 63 | + fs.writeFileSync(filePath, 'this is the previous file', 'utf-8') |
| 64 | + |
| 65 | + await plugin.fullTest([ |
| 66 | + { type: 'file', path: filePath, contents: 'anything', onlyCreate: true } |
| 67 | + ], { |
| 68 | + skipUninstall: true, |
| 69 | + validatePlan(plans) { |
| 70 | + expect(plans[0]).toMatchObject({ |
| 71 | + operation: ResourceOperation.NOOP, |
| 72 | + }) |
| 73 | + } |
| 74 | + }) |
| 75 | + |
| 76 | + expect(fs.readFileSync(filePath, 'utf-8')).to.eq('this is the previous file'); |
| 77 | + }) |
| 78 | +}) |
0 commit comments