|
4 | 4 | * SPDX-License-Identifier: Apache-2.0 |
5 | 5 | */ |
6 | 6 | import assert from 'node:assert'; |
7 | | -import {rm, stat, mkdir, chmod} from 'node:fs/promises'; |
| 7 | +import {rm, stat, mkdir, chmod, writeFile} from 'node:fs/promises'; |
8 | 8 | import {tmpdir} from 'node:os'; |
9 | 9 | import {join} from 'node:path'; |
10 | 10 | import {describe, it} from 'node:test'; |
@@ -145,27 +145,53 @@ describe('screenshot', () => { |
145 | 145 | }); |
146 | 146 |
|
147 | 147 | it('with unwritable filePath', async () => { |
148 | | - const dir = join(tmpdir(), 'readonly-dir-for-screenshot-test'); |
149 | | - await mkdir(dir, {recursive: true}); |
150 | | - await chmod(dir, 0o500); |
151 | | - const filePath = join(dir, 'test-screenshot.png'); |
| 148 | + if (process.platform === 'win32') { |
| 149 | + const filePath = join(tmpdir(), 'readonly-file-for-screenshot-test.png'); |
| 150 | + // Create the file and make it read-only. |
| 151 | + await writeFile(filePath, ''); |
| 152 | + await chmod(filePath, 0o400); |
152 | 153 |
|
153 | | - try { |
154 | | - await withBrowser(async (response, context) => { |
155 | | - const fixture = screenshots.basic; |
156 | | - const page = context.getSelectedPage(); |
157 | | - await page.setContent(fixture.html); |
158 | | - await assert.rejects( |
159 | | - screenshot.handler( |
160 | | - {params: {format: 'png', filePath}}, |
161 | | - response, |
162 | | - context, |
163 | | - ), |
164 | | - ); |
165 | | - }); |
166 | | - } finally { |
167 | | - await chmod(dir, 0o700); |
168 | | - await rm(dir, {recursive: true, force: true}); |
| 154 | + try { |
| 155 | + await withBrowser(async (response, context) => { |
| 156 | + const fixture = screenshots.basic; |
| 157 | + const page = context.getSelectedPage(); |
| 158 | + await page.setContent(fixture.html); |
| 159 | + await assert.rejects( |
| 160 | + screenshot.handler( |
| 161 | + {params: {format: 'png', filePath}}, |
| 162 | + response, |
| 163 | + context, |
| 164 | + ), |
| 165 | + ); |
| 166 | + }); |
| 167 | + } finally { |
| 168 | + // Make the file writable again so it can be deleted. |
| 169 | + await chmod(filePath, 0o600); |
| 170 | + await rm(filePath, {force: true}); |
| 171 | + } |
| 172 | + } else { |
| 173 | + const dir = join(tmpdir(), 'readonly-dir-for-screenshot-test'); |
| 174 | + await mkdir(dir, {recursive: true}); |
| 175 | + await chmod(dir, 0o500); |
| 176 | + const filePath = join(dir, 'test-screenshot.png'); |
| 177 | + |
| 178 | + try { |
| 179 | + await withBrowser(async (response, context) => { |
| 180 | + const fixture = screenshots.basic; |
| 181 | + const page = context.getSelectedPage(); |
| 182 | + await page.setContent(fixture.html); |
| 183 | + await assert.rejects( |
| 184 | + screenshot.handler( |
| 185 | + {params: {format: 'png', filePath}}, |
| 186 | + response, |
| 187 | + context, |
| 188 | + ), |
| 189 | + ); |
| 190 | + }); |
| 191 | + } finally { |
| 192 | + await chmod(dir, 0o700); |
| 193 | + await rm(dir, {recursive: true, force: true}); |
| 194 | + } |
169 | 195 | } |
170 | 196 | }); |
171 | 197 |
|
|
0 commit comments