Skip to content

Commit 4a35bab

Browse files
authored
ci: missing CLI tests in matrix (#264)
* ci: missing CLI tests in matrix * ci: use package name in matrix * ci: make sure coverage is separate * test: fix watch test * build: fix types * test: final fixes
1 parent fb10e4b commit 4a35bab

File tree

7 files changed

+41
-13
lines changed

7 files changed

+41
-13
lines changed

.github/workflows/tests.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ jobs:
1313
matrix:
1414
node: [ 18, 20, "lts/*" ]
1515
package:
16+
- name: kopflos
17+
path: packages/cli
1618
- name: "@kopflos-cms/core"
1719
path: packages/core
1820
- name: "@kopflos-cms/express"
@@ -40,7 +42,7 @@ jobs:
4042
with:
4143
node-version: ${{ matrix.node }}
4244
- run: npm ci
43-
- run: npx c8 --all --src ${{ matrix.package.path }} --reporter lcovonly --reporter text npm run -w ${{ matrix.package.path }} test
45+
- run: npx c8 --all --src ${{ matrix.package.path }} --include ${{ matrix.package.path }} --reporter lcovonly --reporter text npm run -w ${{ matrix.package.name }} test
4446
- name: Codecov
4547
uses: codecov/codecov-action@v4
4648
with:

packages/cli/lib/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export async function loadConfig({ path, root }: LoadConfig): Promise<{ config:
3232
const { config: { plugins, ...config }, ...rest } = ccResult
3333
return {
3434
config: {
35-
plugins: Array.isArray(plugins) ? plugins : await loadPlugins(plugins),
35+
plugins: Array.isArray(plugins) ? plugins : await loadPlugins(dirname(rest.filepath), plugins),
3636
...config,
3737
},
3838
...rest,

packages/cli/lib/plugins.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
import module from 'node:module'
12
import type { KopflosPlugin } from '@kopflos-cms/core'
23
import { createLogger } from '@kopflos-cms/logger'
34

5+
const require = module.createRequire(import.meta.url)
6+
47
type KopflosPluginConstructor = new (options: unknown) => KopflosPlugin
58

69
const log = createLogger('kopflos')
710

8-
export async function loadPlugins(plugins: Record<string, unknown>): Promise<KopflosPlugin[]> {
11+
export async function loadPlugins(root: string, plugins: Record<string, unknown> = {}): Promise<KopflosPlugin[]> {
912
const pluginsCombined = Object.entries(plugins).filter(([plugin, options]) => {
1013
if (options === false) {
1114
log.debug('Skipping disabled plugin', plugin)
@@ -18,7 +21,12 @@ export async function loadPlugins(plugins: Record<string, unknown>): Promise<Kop
1821
return Promise.all(pluginsCombined.map(async ([plugin, options]) => {
1922
log.info('Loading plugin', plugin)
2023

21-
const [module, exportName = 'default'] = plugin.split('#')
24+
let [module, exportName = 'default'] = plugin.split('#')
25+
26+
// if module is relative, resolve it relative to the root of the project
27+
if (module.startsWith('.')) {
28+
module = require.resolve(module, { paths: [root] })
29+
}
2230

2331
const Plugin: KopflosPluginConstructor = (await import(module))[exportName]
2432
return new Plugin(options)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default class {}

packages/cli/test/lib/command/serve.test.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,23 @@ describe('kopflos/lib/command/serve', function () {
1414

1515
beforeEach(createEmpty)
1616

17+
before(function () {
18+
if (!fs.existsSync(fixturesDir)) {
19+
fs.mkdirSync(fixturesDir)
20+
}
21+
})
22+
1723
beforeEach(function () {
18-
process = fork(serve)
19-
fs.mkdirSync(fixturesDir)
24+
process = fork(serve, {
25+
execArgv: ['--import', 'tsx'],
26+
})
2027
})
2128

2229
afterEach(function () {
2330
process.kill()
31+
})
32+
33+
after(function () {
2434
fs.rmSync(fixturesDir, { recursive: true, force: true })
2535
})
2636

@@ -39,7 +49,10 @@ describe('kopflos/lib/command/serve', function () {
3949
}
4050
})
4151

42-
fs.writeFileSync(new URL('./file.txt', fixturesDir), '')
52+
new Promise(resolve => setTimeout(resolve, 1000))
53+
.then(() => {
54+
fs.writeFileSync(new URL('./file.txt', fixturesDir), '')
55+
})
4356
}))
4457
})
4558

@@ -76,7 +89,10 @@ describe('kopflos/lib/command/serve', function () {
7689
}
7790
})
7891

79-
fs.writeFileSync(new URL('./file.txt', fixturesDir), '')
92+
new Promise(resolve => setTimeout(resolve, 1000))
93+
.then(() => {
94+
fs.writeFileSync(new URL('./file.txt', fixturesDir), '')
95+
})
8096
}))
8197
})
8298

packages/cli/test/lib/config.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import url from 'node:url'
22
import { expect } from 'chai'
33
import { loadConfig, prepareConfig } from '../../lib/config.js'
4+
import BarPlugin from '../fixtures/foo/bar.js'
45

56
describe('kopflos/lib/config.js', function () {
67
this.timeout(10000)
@@ -29,6 +30,7 @@ describe('kopflos/lib/config.js', function () {
2930
// given
3031
expect(config).to.be.deep.equal({
3132
baseIri: 'https://example.com/',
33+
plugins: [],
3234
})
3335
})
3436
})
@@ -79,9 +81,8 @@ describe('kopflos/lib/config.js', function () {
7981
})
8082

8183
// then
82-
expect(config.plugins).to.contain.keys([
83-
url.fileURLToPath(new URL('../fixtures/foo/bar.js', import.meta.url)),
84-
])
84+
expect(config.plugins).to.have.length(1)
85+
expect(config.plugins![0]).to.be.instanceOf(BarPlugin)
8586
})
8687
})
8788
})

packages/plugin-deploy-resources/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ interface Options {
1313
}
1414

1515
interface DeployResourcesPlugin extends KopflosPlugin {
16-
deploy(env: KopflosEnvironment, plugins: KopflosPlugin[]): Promise<void>
16+
deploy(env: KopflosEnvironment, plugins: readonly KopflosPlugin[]): Promise<void>
1717
}
1818

1919
declare module '@kopflos-cms/core' {
@@ -74,7 +74,7 @@ export default class implements DeployResourcesPlugin {
7474
return this.deploy(instance.env, instance.plugins)
7575
}
7676

77-
async deploy(env: KopflosEnvironment, plugins: KopflosPlugin[]) {
77+
async deploy(env: KopflosEnvironment, plugins: readonly KopflosPlugin[]) {
7878
const dataset = await plugins.reduce(async (promise, plugin) => {
7979
if (!plugin.deployedResources) {
8080
return promise

0 commit comments

Comments
 (0)