1 import { exec } from 'child_process'
2 import { copy, ensureDir, readFile, remove } from 'fs-extra'
3 import { join } from 'path'
4 import { root } from '@server/helpers/core-utils'
5 import { HttpStatusCode } from '@shared/core-utils'
6 import { getFileSize } from '@uploadx/core'
7 import { isGithubCI, wait } from '../miscs'
8 import { AbstractCommand, OverrideCommandOptions } from '../shared'
10 export class ServersCommand extends AbstractCommand {
12 static flushTests (internalServerNumber: number) {
13 return new Promise<void>((res, rej) => {
14 const suffix = ` -- ${internalServerNumber}`
16 return exec('npm run clean:server:test' + suffix, (err, _stdout, stderr) => {
17 if (err || stderr) return rej(err || new Error(stderr))
24 ping (options: OverrideCommandOptions = {}) {
25 return this.getRequestBody({
30 defaultExpectedStatus: HttpStatusCode.OK_200
34 async cleanupTests () {
35 const p: Promise<any>[] = []
38 await ensureDir('artifacts')
40 const origin = this.server.serversCommand.buildDirectory('logs/peertube.log')
41 const destname = `peertube-${this.server.internalServerNumber}.log`
42 console.log('Saving logs %s.', destname)
44 await copy(origin, join('artifacts', destname))
47 if (this.server.parallel) {
48 p.push(ServersCommand.flushTests(this.server.internalServerNumber))
51 if (this.server.customConfigFile) {
52 p.push(remove(this.server.customConfigFile))
58 async waitUntilLog (str: string, count = 1, strictCount = true) {
59 const logfile = this.server.serversCommand.buildDirectory('logs/peertube.log')
62 const buf = await readFile(logfile)
64 const matches = buf.toString().match(new RegExp(str, 'g'))
65 if (matches && matches.length === count) return
66 if (matches && strictCount === false && matches.length >= count) return
72 buildDirectory (directory: string) {
73 return join(root(), 'test' + this.server.internalServerNumber, directory)
76 async getServerFileSize (subPath: string) {
77 const path = this.server.serversCommand.buildDirectory(subPath)
79 return getFileSize(path)