|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const fs = require('node:fs') |
| 4 | +const path = require('node:path') |
| 5 | +const { describe, it, before } = require('node:test') |
| 6 | +const assert = require('node:assert') |
| 7 | +const { execSync } = require('node:child_process') |
| 8 | +const cds = require('@sap/cds') |
| 9 | + |
| 10 | +const dir = path.join(__dirname, 'dummy-project') |
| 11 | +// use a slightly different model directory to avoid race-conditions with |
| 12 | +// vscode, as our extension eagerly creates @cds-models when creating a new .cds file |
| 13 | +// as part of our tests |
| 14 | +const modelsDir = path.join(dir, '@cds-models-test') |
| 15 | +const logs = [] |
| 16 | + |
| 17 | +describe('cds watch', () => { |
| 18 | + before(async () => { |
| 19 | + // spy on cds.debug('cli|build') |
| 20 | + const cdsLog = cds.debug |
| 21 | + cds.debug = (...args) => { |
| 22 | + return args[0] === 'cli|build' |
| 23 | + ? (...largs) => { |
| 24 | + logs.push(args, largs) |
| 25 | + return cdsLog(...args)(...largs) |
| 26 | + } |
| 27 | + : cdsLog(...args)} |
| 28 | + }) |
| 29 | + |
| 30 | + it('runs once before cds watch', async () => { |
| 31 | + await fs.promises.rm(modelsDir, { recursive: true, force: true }) |
| 32 | + cds.watched = true |
| 33 | + cds.root = dir |
| 34 | + // need to set manually, as we don't go through the regular cds bootstrap |
| 35 | + cds.env.typer = { output_directory: modelsDir } |
| 36 | + cds.env.log.levels.cli = 'debug' |
| 37 | + await require('../../cds-plugin') |
| 38 | + assert.ok(logs.some(log => log.some(line => line.match(/start.*cds-typer/)))) |
| 39 | + logs.length = 0 |
| 40 | + await require('../../cds-plugin') |
| 41 | + // should not run when models are already there |
| 42 | + assert.ok(!logs.some(log => log.some(line => line.match(/end.*cds-typer/)))) |
| 43 | + }) |
| 44 | +}) |
0 commit comments