|
3 | 3 | * SPDX-License-Identifier: AGPL-3.0-or-later |
4 | 4 | */ |
5 | 5 |
|
6 | | -import { getContainer } from '@nextcloud/cypress/docker' |
| 6 | +import { runExec, addUser } from '@nextcloud/cypress/docker' |
7 | 7 | import { expect, type APIRequestContext } from '@playwright/test' |
8 | 8 |
|
9 | | -/** |
10 | | - * Run a shell command on the docker container |
11 | | - * @param command The command to run on the docker container |
12 | | - * @param options Options to pass |
13 | | - * @param options.env Process environment to pass |
14 | | - * @param options.user User to use for executing the command |
15 | | - * @param options.rejectOnError Reject the returned promise in case of non-zero exit code |
16 | | - */ |
17 | | -export async function runShell( |
18 | | - command: string, |
19 | | - options?: { |
20 | | - user?: string |
21 | | - rejectOnError?: boolean |
22 | | - env?: Record<string, string | number> |
23 | | - }, |
24 | | -) { |
25 | | - const container = getContainer() |
26 | | - |
27 | | - const exec = await container.exec({ |
28 | | - Cmd: ['sh', '-c', command], |
29 | | - Env: Object.entries(options?.env ?? {}).map( |
30 | | - ([name, value]) => `${name}=${value}`, |
31 | | - ), |
32 | | - User: options?.user, |
33 | | - AttachStderr: true, |
34 | | - AttachStdout: true, |
35 | | - }) |
36 | | - |
37 | | - const stream = await exec.start({}) |
38 | | - return new Promise((resolve, reject) => { |
39 | | - let data = '' |
40 | | - stream.on('data', (chunk: string) => { |
41 | | - data += chunk |
42 | | - }) |
43 | | - stream.on('error', (error: unknown) => reject(error)) |
44 | | - stream.on('end', async () => { |
45 | | - const inspect = await exec.inspect({}) |
46 | | - if (options?.rejectOnError !== false && inspect.ExitCode) { |
47 | | - reject(data) |
48 | | - } else { |
49 | | - resolve(data) |
50 | | - } |
51 | | - }) |
52 | | - }) |
53 | | -} |
54 | | - |
55 | | -/** |
56 | | - * Run an OCC command |
57 | | - * @param command OCC command to run |
58 | | - * @param options Options to pass |
59 | | - * @param options.env Process environment to pass |
60 | | - * @param options.rejectOnError Reject the returned promise in case of non-zero exit code |
61 | | - */ |
62 | | -export async function runOCC( |
63 | | - command: string, |
64 | | - options?: { |
65 | | - env?: Record<string, string | number> |
66 | | - rejectOnError?: boolean |
67 | | - }, |
68 | | -) { |
69 | | - return await runShell(`php ./occ ${command}`, { |
70 | | - ...options, |
71 | | - user: 'www-data', |
72 | | - }) |
73 | | -} |
74 | | - |
75 | 9 | /** |
76 | 10 | * Restore database and data folder for tests |
77 | 11 | */ |
78 | 12 | export function restoreDatabase() { |
79 | | - runShell('rm -rf data && tar -xf backup.tar') |
| 13 | + runExec('rm -rf data && tar -xf backup.tar') |
80 | 14 | } |
81 | 15 |
|
82 | 16 | /** |
@@ -116,8 +50,6 @@ export async function login( |
116 | 50 | */ |
117 | 51 | export async function createRandomUser(): Promise<string> { |
118 | 52 | const uid = (Math.random() + 1).toString(36).substring(7) |
119 | | - await runOCC(`user:add --password-from-env ${uid}`, { |
120 | | - env: { OC_PASS: uid }, |
121 | | - }) |
| 53 | + await addUser(uid) |
122 | 54 | return uid |
123 | 55 | } |
0 commit comments