test: __dirnameはESModuleでは使えないので置き換えた (#8626)

This commit is contained in:
iwata 2022-05-14 16:09:47 +09:00 committed by GitHub
parent b2a5076d14
commit ebb4308a5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 53 additions and 23 deletions

View File

@ -16,6 +16,17 @@ module.exports = {
'position': 'after'
}
],
}]
}],
'no-restricted-globals': [
'error',
{
'name': '__dirname',
'message': 'Not in ESModule. Use `import.meta.url` instead.'
},
{
'name': '__filename',
'message': 'Not in ESModule. Use `import.meta.url` instead.'
}
]
},
};

View File

@ -1,7 +0,0 @@
{
"env": {
"node": true,
"mocha": true,
"commonjs": true
}
}

View File

@ -0,0 +1,11 @@
module.exports = {
parserOptions: {
tsconfigRootDir: __dirname,
project: ['./tsconfig.json'],
},
extends: ['../.eslintrc.cjs'],
env: {
node: true,
mocha: true,
},
};

View File

@ -1,10 +1,15 @@
import * as assert from 'assert';
import { async } from './utils.js';
import { fileURLToPath } from 'node:url';
import { dirname } from 'node:path';
import { getFileInfo } from '../src/misc/get-file-info.js';
import { async } from './utils.js';
const _filename = fileURLToPath(import.meta.url);
const _dirname = dirname(_filename);
describe('Get file info', () => {
it('Empty file', async (async () => {
const path = `${__dirname}/resources/emptyfile`;
const path = `${_dirname}/resources/emptyfile`;
const info = await getFileInfo(path) as any;
delete info.warnings;
delete info.blurhash;
@ -22,7 +27,7 @@ describe('Get file info', () => {
}));
it('Generic JPEG', async (async () => {
const path = `${__dirname}/resources/Lenna.jpg`;
const path = `${_dirname}/resources/Lenna.jpg`;
const info = await getFileInfo(path) as any;
delete info.warnings;
delete info.blurhash;
@ -40,7 +45,7 @@ describe('Get file info', () => {
}));
it('Generic APNG', async (async () => {
const path = `${__dirname}/resources/anime.png`;
const path = `${_dirname}/resources/anime.png`;
const info = await getFileInfo(path) as any;
delete info.warnings;
delete info.blurhash;
@ -58,7 +63,7 @@ describe('Get file info', () => {
}));
it('Generic AGIF', async (async () => {
const path = `${__dirname}/resources/anime.gif`;
const path = `${_dirname}/resources/anime.gif`;
const info = await getFileInfo(path) as any;
delete info.warnings;
delete info.blurhash;
@ -76,7 +81,7 @@ describe('Get file info', () => {
}));
it('PNG with alpha', async (async () => {
const path = `${__dirname}/resources/with-alpha.png`;
const path = `${_dirname}/resources/with-alpha.png`;
const info = await getFileInfo(path) as any;
delete info.warnings;
delete info.blurhash;
@ -94,7 +99,7 @@ describe('Get file info', () => {
}));
it('Generic SVG', async (async () => {
const path = `${__dirname}/resources/image.svg`;
const path = `${_dirname}/resources/image.svg`;
const info = await getFileInfo(path) as any;
delete info.warnings;
delete info.blurhash;
@ -113,7 +118,7 @@ describe('Get file info', () => {
it('SVG with XML definition', async (async () => {
// https://github.com/misskey-dev/misskey/issues/4413
const path = `${__dirname}/resources/with-xml-def.svg`;
const path = `${_dirname}/resources/with-xml-def.svg`;
const info = await getFileInfo(path) as any;
delete info.warnings;
delete info.blurhash;
@ -131,7 +136,7 @@ describe('Get file info', () => {
}));
it('Dimension limit', async (async () => {
const path = `${__dirname}/resources/25000x25000.png`;
const path = `${_dirname}/resources/25000x25000.png`;
const info = await getFileInfo(path) as any;
delete info.warnings;
delete info.blurhash;
@ -149,7 +154,7 @@ describe('Get file info', () => {
}));
it('Rotate JPEG', async (async () => {
const path = `${__dirname}/resources/rotate.jpg`;
const path = `${_dirname}/resources/rotate.jpg`;
const info = await getFileInfo(path) as any;
delete info.warnings;
delete info.blurhash;

View File

@ -2,8 +2,13 @@ process.env.NODE_ENV = 'test';
import * as assert from 'assert';
import * as childProcess from 'child_process';
import { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { async, signup, request, post, uploadFile, startServer, shutdownServer } from './utils.js';
const _filename = fileURLToPath(import.meta.url);
const _dirname = dirname(_filename);
describe('users/notes', () => {
let p: childProcess.ChildProcess;
@ -15,8 +20,8 @@ describe('users/notes', () => {
before(async () => {
p = await startServer();
alice = await signup({ username: 'alice' });
const jpg = await uploadFile(alice, __dirname + '/resources/Lenna.jpg');
const png = await uploadFile(alice, __dirname + '/resources/Lenna.png');
const jpg = await uploadFile(alice, _dirname + '/resources/Lenna.jpg');
const png = await uploadFile(alice, _dirname + '/resources/Lenna.png');
jpgNote = await post(alice, {
fileIds: [jpg.id]
});

View File

@ -1,4 +1,6 @@
import * as fs from 'node:fs';
import { fileURLToPath } from 'node:url';
import { dirname } from 'node:path';
import * as WebSocket from 'ws';
import * as misskey from 'misskey-js';
import fetch from 'node-fetch';
@ -9,6 +11,9 @@ import loadConfig from '../src/config/load.js';
import { SIGKILL } from 'constants';
import { entities } from '../src/db/postgre.js';
const _filename = fileURLToPath(import.meta.url);
const _dirname = dirname(_filename);
const config = loadConfig();
export const port = config.port;
@ -72,7 +77,7 @@ export const react = async (user: any, note: any, reaction: string): Promise<any
export const uploadFile = (user: any, path?: string): Promise<any> => {
const formData = new FormData();
formData.append('i', user.token);
formData.append('file', fs.createReadStream(path || __dirname + '/resources/Lenna.png'));
formData.append('file', fs.createReadStream(path || _dirname + '/resources/Lenna.png'));
return fetch(`http://localhost:${port}/api/drive/files/create`, {
method: 'post',
@ -139,7 +144,7 @@ export const simpleGet = async (path: string, accept = '*/*'): Promise<{ status?
export function launchServer(callbackSpawnedProcess: (p: childProcess.ChildProcess) => void, moreProcess: () => Promise<void> = async () => {}) {
return (done: (err?: Error) => any) => {
const p = childProcess.spawn('node', [__dirname + '/../index.js'], {
const p = childProcess.spawn('node', [_dirname + '/../index.js'], {
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
env: { NODE_ENV: 'test', PATH: process.env.PATH }
});
@ -178,7 +183,7 @@ export function startServer(timeout = 30 * 1000): Promise<childProcess.ChildProc
rej('timeout to start');
}, timeout);
const p = childProcess.spawn('node', [__dirname + '/../built/index.js'], {
const p = childProcess.spawn('node', [_dirname + '/../built/index.js'], {
stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
env: { NODE_ENV: 'test', PATH: process.env.PATH }
});