Skip to content

Commit

Permalink
test(e2e): use @nextcloud/cypress/docker functions
Browse files Browse the repository at this point in the history
Signed-off-by: Max <[email protected]>
  • Loading branch information
max-nextcloud committed Jan 9, 2025
1 parent df836b5 commit d79bd6c
Showing 1 changed file with 3 additions and 71 deletions.
74 changes: 3 additions & 71 deletions playwright/support/utils/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,80 +3,14 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { getContainer } from '@nextcloud/cypress/docker'
import { runExec, addUser } from '@nextcloud/cypress/docker'
import { expect, type APIRequestContext } from '@playwright/test'

/**
* Run a shell command on the docker container
* @param command The command to run on the docker container
* @param options Options to pass
* @param options.env Process environment to pass
* @param options.user User to use for executing the command
* @param options.rejectOnError Reject the returned promise in case of non-zero exit code
*/
export async function runShell(
command: string,
options?: {
user?: string
rejectOnError?: boolean
env?: Record<string, string | number>
},
) {
const container = getContainer()

const exec = await container.exec({
Cmd: ['sh', '-c', command],
Env: Object.entries(options?.env ?? {}).map(
([name, value]) => `${name}=${value}`,
),
User: options?.user,
AttachStderr: true,
AttachStdout: true,
})

const stream = await exec.start({})
return new Promise((resolve, reject) => {
let data = ''
stream.on('data', (chunk: string) => {
data += chunk
})
stream.on('error', (error: unknown) => reject(error))
stream.on('end', async () => {
const inspect = await exec.inspect({})
if (options?.rejectOnError !== false && inspect.ExitCode) {
reject(data)
} else {
resolve(data)
}
})
})
}

/**
* Run an OCC command
* @param command OCC command to run
* @param options Options to pass
* @param options.env Process environment to pass
* @param options.rejectOnError Reject the returned promise in case of non-zero exit code
*/
export async function runOCC(
command: string,
options?: {
env?: Record<string, string | number>
rejectOnError?: boolean
},
) {
return await runShell(`php ./occ ${command}`, {
...options,
user: 'www-data',
})
}

/**
* Restore database and data folder for tests
*/
export function restoreDatabase() {
runShell('rm -rf data && tar -xf backup.tar')
runExec('rm -rf data && tar -xf backup.tar')
}

/**
Expand Down Expand Up @@ -116,8 +50,6 @@ export async function login(
*/
export async function createRandomUser(): Promise<string> {
const uid = (Math.random() + 1).toString(36).substring(7)
await runOCC(`user:add --password-from-env ${uid}`, {
env: { OC_PASS: uid },
})
await addUser(uid)
return uid
}

0 comments on commit d79bd6c

Please sign in to comment.