Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests: use nc docker functions in playwright #2484

Merged
merged 2 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"@mdi/js": "^7.4.47",
"@mdi/svg": "^7.4.47",
"@nextcloud/browserslist-config": "^3.0.1",
"@nextcloud/cypress": "^1.0.0-beta.12",
"@nextcloud/cypress": "^1.0.0-beta.13",
"@nextcloud/eslint-config": "^8.4.1",
"@nextcloud/prettier-config": "^1.1.0",
"@nextcloud/stylelint-config": "^3.0.1",
Expand Down
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
}
Loading