-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathpluginManager.test.ts
More file actions
91 lines (82 loc) · 3.19 KB
/
Copy pathpluginManager.test.ts
File metadata and controls
91 lines (82 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import {describe, it, expect, vi, beforeEach} from 'vitest'
import * as fs from 'fs'
import * as dynamicImportModule from '../src/dynamicImport.js'
import * as pluginManager from '../src/pluginManager.js'
import * as core from '@actions/core'
// - enable spying on fs
// https://vitest.dev/guide/browser/#limitations
vi.mock('fs', {spy: true})
vi.mock('../src/pluginManager.js', {spy: true})
vi.mock('@actions/core', {spy: true})
describe('loadPlugins', () => {
let dynamicImportCallCount = 0
vi.spyOn(dynamicImportModule, 'dynamicImport').mockImplementation(() => {
dynamicImportCallCount++
return Promise.resolve({name: `plugin-${dynamicImportCallCount}`, default: vi.fn()})
})
beforeEach(() => {
dynamicImportCallCount = 0
// @ts-expect-error - we don't need the full fs readdirsync
// method signature here
vi.spyOn(fs, 'readdirSync').mockImplementation(() => {
return ['folder-a', 'folder-b']
})
vi.spyOn(fs, 'lstatSync').mockImplementation(() => {
return {
isDirectory: () => true,
} as unknown as fs.Stats
})
vi.spyOn(fs, 'existsSync').mockImplementation(() => true)
})
describe('when plugins are not loaded', () => {
it('loads them', async () => {
pluginManager.clearCache()
const plugins = await pluginManager.loadPlugins()
expect(dynamicImportModule.dynamicImport).toHaveBeenCalledTimes(4)
expect(plugins.length).toBe(4)
})
})
describe('when plugins are already loaded', () => {
it('caches them and doesnt load them again', async () => {
pluginManager.clearCache()
await pluginManager.loadPlugins()
await pluginManager.loadPlugins()
expect(pluginManager.loadBuiltInPlugins).toHaveBeenCalledTimes(0)
expect(pluginManager.loadCustomPlugins).toHaveBeenCalledTimes(0)
})
})
describe('when there is an error loading plugins', () => {
beforeEach(() => {
vi.spyOn(fs, 'readdirSync').mockImplementation(() => {
throw new Error('test error')
})
})
it('Aborts loading all plugins', async () => {
pluginManager.clearCache()
const logSpy = vi.spyOn(core, 'error').mockImplementation(() => {})
const plugins = await pluginManager.loadPlugins()
expect(plugins.length).toBe(0)
expect(logSpy).toHaveBeenCalledWith(pluginManager.abortError)
})
})
describe('when built-in and custom plugins share the same name', () => {
beforeEach(() => {
// @ts-expect-error - we don't need the full fs readdirsync
// method signature here
vi.spyOn(fs, 'readdirSync').mockImplementation(() => {
return ['reflow-scan']
})
vi.spyOn(dynamicImportModule, 'dynamicImport').mockImplementation(() => {
return Promise.resolve({name: 'reflow-scan', default: vi.fn()})
})
})
it('skips the duplicate and only loads the plugin once', async () => {
pluginManager.clearCache()
const infoSpy = vi.spyOn(core, 'info').mockImplementation(() => {})
const plugins = await pluginManager.loadPlugins()
expect(plugins.length).toBe(1)
expect(plugins[0].name).toBe('reflow-scan')
expect(infoSpy).toHaveBeenCalledWith('Skipping duplicate plugin: reflow-scan')
})
})
})