|
| 1 | +import { readFile, rm, writeFile } from 'node:fs/promises'; |
| 2 | +import path from 'node:path'; |
| 3 | +import process from 'node:process'; |
| 4 | + |
| 5 | +import replaceBackslashes from '../testUtils/replaceBackslashes.mjs'; |
| 6 | +import safeChdir from '../testUtils/safeChdir.mjs'; |
| 7 | +import standalone from '../standalone.mjs'; |
| 8 | +import uniqueId from '../testUtils/uniqueId.mjs'; |
| 9 | + |
| 10 | +const fixturesPath = replaceBackslashes(new URL('./fixtures', import.meta.url)); |
| 11 | + |
| 12 | +describe('standalone with abortSignal', () => { |
| 13 | + describe('using code', () => { |
| 14 | + it('completes normally when signal is not aborted', async () => { |
| 15 | + const controller = new AbortController(); |
| 16 | + |
| 17 | + const { results } = await standalone({ |
| 18 | + code: 'a {}', |
| 19 | + config: { rules: { 'block-no-empty': true } }, |
| 20 | + abortSignal: controller.signal, |
| 21 | + }); |
| 22 | + |
| 23 | + expect(results).toHaveLength(1); |
| 24 | + expect(results[0].warnings).toHaveLength(1); |
| 25 | + expect(results[0].warnings[0].rule).toBe('block-no-empty'); |
| 26 | + }); |
| 27 | + |
| 28 | + it('rejects when signal is already aborted', async () => { |
| 29 | + const controller = new AbortController(); |
| 30 | + |
| 31 | + controller.abort(); |
| 32 | + |
| 33 | + await expect( |
| 34 | + standalone({ |
| 35 | + code: 'a {}', |
| 36 | + config: { rules: { 'block-no-empty': true } }, |
| 37 | + abortSignal: controller.signal, |
| 38 | + }), |
| 39 | + ).rejects.toThrow('This operation was aborted'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('rejects with a custom abort reason', async () => { |
| 43 | + const controller = new AbortController(); |
| 44 | + |
| 45 | + controller.abort(new Error('Lint cancelled')); |
| 46 | + |
| 47 | + await expect( |
| 48 | + standalone({ |
| 49 | + code: 'a {}', |
| 50 | + config: { rules: { 'block-no-empty': true } }, |
| 51 | + abortSignal: controller.signal, |
| 52 | + }), |
| 53 | + ).rejects.toThrow('Lint cancelled'); |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('using files', () => { |
| 58 | + it('completes normally when signal is not aborted', async () => { |
| 59 | + const controller = new AbortController(); |
| 60 | + |
| 61 | + const { results } = await standalone({ |
| 62 | + files: `${fixturesPath}/empty-block.css`, |
| 63 | + config: { rules: { 'block-no-empty': true } }, |
| 64 | + abortSignal: controller.signal, |
| 65 | + }); |
| 66 | + |
| 67 | + expect(results).toHaveLength(1); |
| 68 | + expect(results[0].warnings).toHaveLength(1); |
| 69 | + expect(results[0].warnings[0].rule).toBe('block-no-empty'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('rejects when signal is already aborted', async () => { |
| 73 | + const controller = new AbortController(); |
| 74 | + |
| 75 | + controller.abort(); |
| 76 | + |
| 77 | + await expect( |
| 78 | + standalone({ |
| 79 | + files: `${fixturesPath}/empty-block.css`, |
| 80 | + config: { rules: { 'block-no-empty': true } }, |
| 81 | + abortSignal: controller.signal, |
| 82 | + }), |
| 83 | + ).rejects.toThrow('This operation was aborted'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('rejects with a custom abort reason', async () => { |
| 87 | + const controller = new AbortController(); |
| 88 | + |
| 89 | + controller.abort(new Error('Lint cancelled')); |
| 90 | + |
| 91 | + await expect( |
| 92 | + standalone({ |
| 93 | + files: `${fixturesPath}/empty-block.css`, |
| 94 | + config: { rules: { 'block-no-empty': true } }, |
| 95 | + abortSignal: controller.signal, |
| 96 | + }), |
| 97 | + ).rejects.toThrow('Lint cancelled'); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + describe('with fix and code', () => { |
| 102 | + it('completes normally when signal is not aborted', async () => { |
| 103 | + const controller = new AbortController(); |
| 104 | + |
| 105 | + const { code } = await standalone({ |
| 106 | + code: 'a { color: #ffffff; }', |
| 107 | + config: { rules: { 'color-hex-length': 'short' } }, |
| 108 | + fix: true, |
| 109 | + abortSignal: controller.signal, |
| 110 | + }); |
| 111 | + |
| 112 | + expect(code).toBeDefined(); |
| 113 | + }); |
| 114 | + |
| 115 | + it('rejects when signal is already aborted', async () => { |
| 116 | + const controller = new AbortController(); |
| 117 | + |
| 118 | + controller.abort(); |
| 119 | + |
| 120 | + await expect( |
| 121 | + standalone({ |
| 122 | + code: 'a { color: #ffffff; }', |
| 123 | + config: { rules: { 'color-hex-length': 'short' } }, |
| 124 | + fix: true, |
| 125 | + abortSignal: controller.signal, |
| 126 | + }), |
| 127 | + ).rejects.toThrow('This operation was aborted'); |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + describe('with fix and files', () => { |
| 132 | + safeChdir(new URL(`./tmp/standalone-abort-signal-${uniqueId()}`, import.meta.url)); |
| 133 | + |
| 134 | + let tempFile; |
| 135 | + |
| 136 | + beforeEach(async () => { |
| 137 | + tempFile = replaceBackslashes(path.join(process.cwd(), 'stylesheet.css')); |
| 138 | + await writeFile(tempFile, 'a { color: #ffffff; }'); |
| 139 | + }); |
| 140 | + |
| 141 | + afterEach(async () => { |
| 142 | + await rm(tempFile, { force: true }); |
| 143 | + }); |
| 144 | + |
| 145 | + it('rejects when signal is already aborted', async () => { |
| 146 | + const controller = new AbortController(); |
| 147 | + |
| 148 | + controller.abort(); |
| 149 | + |
| 150 | + const originalContent = await readFile(tempFile, 'utf8'); |
| 151 | + |
| 152 | + await expect( |
| 153 | + standalone({ |
| 154 | + files: tempFile, |
| 155 | + config: { rules: { 'color-hex-length': 'short' } }, |
| 156 | + fix: true, |
| 157 | + abortSignal: controller.signal, |
| 158 | + }), |
| 159 | + ).rejects.toThrow('This operation was aborted'); |
| 160 | + |
| 161 | + const currentContent = await readFile(tempFile, 'utf8'); |
| 162 | + |
| 163 | + expect(currentContent).toBe(originalContent); |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + it('rejects when signal is aborted while processing rules', async () => { |
| 168 | + const controller = new AbortController(); |
| 169 | + |
| 170 | + const aborter = { |
| 171 | + ruleName: 'test/aborter', |
| 172 | + rule: () => async () => { |
| 173 | + controller.abort(new Error('mid-run')); |
| 174 | + }, |
| 175 | + }; |
| 176 | + |
| 177 | + await expect( |
| 178 | + standalone({ |
| 179 | + code: 'a {}', |
| 180 | + config: { plugins: [aborter], rules: { 'test/aborter': true } }, |
| 181 | + abortSignal: controller.signal, |
| 182 | + }), |
| 183 | + ).rejects.toThrow('mid-run'); |
| 184 | + }); |
| 185 | +}); |
0 commit comments