Skip to content

Commit

Permalink
FI-1025 fix: stop docker container if tests are interrupted externally
Browse files Browse the repository at this point in the history
chore: update alpine to 3.18.4 (so update Chrome to 117, etc)
chore: update devDependencies (@types/node)
fix: add `bash` to docker container
refactor: rename files according to the names of exported functions
  • Loading branch information
uid11 committed Nov 24, 2023
1 parent c59ed10 commit 93be1e7
Show file tree
Hide file tree
Showing 29 changed files with 218 additions and 98 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
FROM alpine:3.18.3
FROM alpine:3.18.4

RUN apk --no-cache upgrade && \
apk --no-cache add \
libevent nodejs npm chromium firefox xwininfo xvfb dbus eudev ttf-freefont fluxbox procps tzdata icu-data-full
bash libevent nodejs npm chromium firefox xwininfo xvfb dbus eudev ttf-freefont fluxbox procps tzdata icu-data-full

COPY ./build/node_modules/e2ed /node_modules/e2ed

Expand Down
27 changes: 24 additions & 3 deletions autotests/bin/runDocker.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/env sh
set -e
#!/usr/bin/env bash
set -eo pipefail
set +u

CONTAINER_LABEL="e2ed"
DEBUG_PORT="${E2ED_DOCKER_DEBUG_PORT:-9229}"
DIR="${E2ED_WORKDIR:-$PWD}"
DOCKER_IMAGE=$(grep -m1 dockerImage $DIR/$1 | sed -e "s/^[^'\"\`]*['\"\`]//" -e "s/['\"\`][^'\"\`]*$//")
Expand All @@ -17,16 +18,36 @@ then
exit 9
fi

onExit() {
CONTAINER_ID=$(docker ps --filter "label=$CONTAINER_LABEL" --format "{{.ID}}")

if [ -z $CONTAINER_ID ]
then
echo "Docker container from image $DOCKER_IMAGE:$VERSION already stopped"
else
echo "Stop docker container from image $DOCKER_IMAGE:$VERSION"
docker stop --time=60 $CONTAINER_ID
fi

exit
}

echo "Run docker image $DOCKER_IMAGE:$VERSION"

trap "onExit" EXIT

docker run \
--rm \
$PORT \
--label $CONTAINER_LABEL \
--workdir $DIR \
--volume $MOUNTDIR:$MOUNTDIR \
--env E2ED_ORIGIN=$E2ED_ORIGIN \
--env E2ED_DEBUG=$E2ED_DEBUG \
--env E2ED_DOCKER_DO_AFTER_TESTS=$E2ED_DOCKER_DO_AFTER_TESTS \
--env E2ED_DOCKER_DO_BEFORE_TESTS=$E2ED_DOCKER_DO_BEFORE_TESTS \
--env __INTERNAL_E2ED_PATH_TO_PACK=$1 \
$DOCKER_IMAGE:$VERSION
$DOCKER_IMAGE:$VERSION \
& PID=$!

wait $PID
14 changes: 6 additions & 8 deletions autotests/selectors/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import {createSelectors} from 'e2ed/selectors';

export {inputSelector} from './inputSelector';

export const {createSelector, createSelectorByCss, locatorIdSelector, htmlElementSelector} =
createSelectors({
getLocatorAttributeName: (property) =>
property === 'id' ? 'data-testid' : `data-test-${property}`,
});
export {
createSelector,
createSelectorByCss,
htmlElementSelector,
locatorIdSelector,
} from './selectorFunctions';
10 changes: 10 additions & 0 deletions autotests/selectors/selectorFunctions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {createSelectorFunctions} from 'e2ed/selectors';

/**
* Main functions for creating selectors and working with selectors.
*/
export const {createSelector, createSelectorByCss, locatorIdSelector, htmlElementSelector} =
createSelectorFunctions({
getLocatorAttributeName: (parameter) =>
parameter === 'id' ? 'data-testid' : `data-test-${parameter}`,
});
57 changes: 38 additions & 19 deletions bin/dockerEntrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,40 @@
#!/usr/bin/env sh
#!/usr/bin/env bash
set -eo pipefail
set +u

restoreE2edPackage () {
if [ -d "./node_modules/_e2ed" ]
then
echo "Restore temporarily hiding locally installed e2ed package:"
mv --verbose ./node_modules/_e2ed ./node_modules/e2ed
fi
}

doAfterTests() {
if [ -z $E2ED_DOCKER_DO_AFTER_TESTS ]
then
echo "No after tests script"
else
./autotests/bin/$E2ED_DOCKER_DO_AFTER_TESTS
fi
}

onExit() {
if [[ $PID ]] && ps -p $PID > /dev/null
then
echo "$PID is running"
kill $PID
fi

restoreE2edPackage;
doAfterTests;

echo "Exit from docker entrypoint"
exit
}

trap "onExit" EXIT

if [ -d "./node_modules/e2ed" ]
then
echo "Temporarily hide locally installed e2ed package:"
Expand All @@ -16,24 +50,9 @@ fi

if [ -z $E2ED_DEBUG ]
then
/node_modules/e2ed/bin/runE2edInDockerEnvironment.js
else
node --inspect-brk=0.0.0.0 /node_modules/e2ed/bin/runE2edInDockerEnvironment.js
fi

EXIT_CODE=$?

if [ -d "./node_modules/_e2ed" ]
then
echo "Restore temporarily hiding locally installed e2ed package:"
mv --verbose ./node_modules/_e2ed ./node_modules/e2ed
fi

if [ -z $E2ED_DOCKER_DO_AFTER_TESTS ]
then
echo "No after tests script"
/node_modules/e2ed/bin/runE2edInDockerEnvironment.js & PID=$!
else
./autotests/bin/$E2ED_DOCKER_DO_AFTER_TESTS
node --inspect-brk=0.0.0.0 /node_modules/e2ed/bin/runE2edInDockerEnvironment.js & PID=$!
fi

exit $EXIT_CODE
wait $PID
14 changes: 7 additions & 7 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 @@ -32,7 +32,7 @@
"testcafe-without-typecheck": "3.4.0-rc.1"
},
"devDependencies": {
"@types/node": "20.9.3",
"@types/node": "20.9.5",
"@typescript-eslint/eslint-plugin": "6.12.0",
"@typescript-eslint/parser": "6.12.0",
"assert-modules-support-case-insensitive-fs": "1.0.1",
Expand Down
2 changes: 1 addition & 1 deletion src/context/testRunPromise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {useContext} from '../useContext';
import {assertValueIsDefined, assertValueIsUndefined} from '../utils/asserts';

/**
* Raw versions of getTestRunPromise and setTestRunPromise.
* Raw versions of `getTestRunPromise` and `setTestRunPromise`.
* @internal
*/
const [getRawTestRunPromise, setRawTestRunPromise] = useContext<Promise<undefined>>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import {DESCRIPTION_KEY} from '../constants/internal';

import type {GetLocatorAttributeNameFn, SelectorCustomMethods} from '../types/internal';

/**
* Creates native `e2ed` methods of selector.
* @internal
*/
export const createCustomMethods = (
getLocatorAttributeName: GetLocatorAttributeNameFn,
): SelectorCustomMethods => {
Expand Down
13 changes: 0 additions & 13 deletions src/selectors/createSelectorByCss.ts

This file was deleted.

15 changes: 15 additions & 0 deletions src/selectors/createSelectorByCssCreator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type {CreateSelector, CreateSelectorByCss, Selector} from '../types/internal';

/**
* Creates `createSelectorByCss` function.
* @internal
*/
export const createSelectorByCssCreator = (createSelector: CreateSelector): CreateSelectorByCss => {
/**
* Creates selector of page elements by CSS selector.
*/
const createSelectorByCss = (cssSelectorString: string): Selector =>
createSelector(cssSelectorString);

return createSelectorByCss;
};
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ const createGet = (
return get;
};

/**
* Creates `createSelector` function.
* @internal
*/
export const createSelectorCreator = (customMethods: SelectorCustomMethods): CreateSelector => {
const createSelector: CreateSelector = (...args) => {
const locator = args[0];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import {createSelectorCreator} from './createSelector';
import {createSelectorByCssCreator} from './createSelectorByCss';
import {createCustomMethods} from './customMethods';
import {htmlElementSelectorCreator} from './htmlElementSelector';
import {locatorIdSelectorCreator} from './locatorIdSelector';
import {createCustomMethods} from './createCustomMethods';
import {createSelectorByCssCreator} from './createSelectorByCssCreator';
import {createSelectorCreator} from './createSelectorCreator';
import {htmlElementSelectorCreator} from './htmlElementSelectorCreator';
import {locatorIdSelectorCreator} from './locatorIdSelectorCreator';

import type {
CreateSelectorsOptions,
CreateSelectorFunctionsOptions,
GetLocatorAttributeNameFn,
SelectorCustomMethods,
SelectorFunctions,
} from '../types/internal';

const createSelectorsWithCustomMethods = (
const createSelectorFunctionsWithCustomMethods = (
getLocatorAttributeName: GetLocatorAttributeNameFn,
// force `this` to be Selector
customMethods: SelectorCustomMethods,
): typeof selectorsWithCustomMethods => {
): SelectorFunctions => {
const createSelector = createSelectorCreator(customMethods);

const selectorsWithCustomMethods = {
Expand All @@ -39,9 +40,13 @@ const createSelectorsWithCustomMethods = (
return selectorsWithCustomMethods;
};

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export const createSelectors = ({getLocatorAttributeName}: CreateSelectorsOptions) =>
createSelectorsWithCustomMethods(
/**
* Creates main functions for creating selectors and working with selectors.
*/
export const createSelectorFunctions = ({
getLocatorAttributeName,
}: CreateSelectorFunctionsOptions): SelectorFunctions =>
createSelectorFunctionsWithCustomMethods(
getLocatorAttributeName,
createCustomMethods(getLocatorAttributeName),
);
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type {CreateSelector, Selector} from '../types/internal';

/**
* Creates selector of page HTML element ("documentElement").
* @internal
*/
export const htmlElementSelectorCreator = (createSelector: CreateSelector): Selector =>
/**
* Selector of page HTML element ("documentElement").
*/
createSelector('html');
2 changes: 1 addition & 1 deletion src/selectors/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export {createSelectors} from './createSelectors';
export {createSelectorFunctions} from './createSelectorFunctions';
12 changes: 8 additions & 4 deletions src/selectors/internal.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import {createSelectors} from './createSelectors';
import {createSelectorFunctions} from './createSelectorFunctions';

export const {htmlElementSelector} = createSelectors({
getLocatorAttributeName: (property) =>
property === 'id' ? 'data-testid' : `data-test-${property}`,
/**
* Internal implementation of `htmlElementSelector` selector.
* @internal
*/
export const {htmlElementSelector} = createSelectorFunctions({
getLocatorAttributeName: (parameter) =>
parameter === 'id' ? 'data-testid' : `data-test-${parameter}`,
});
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import type {CreateSelector, GetLocatorAttributeNameFn, Selector} from '../types/internal';
import type {
CreateSelector,
GetLocatorAttributeNameFn,
LocatorIdSelector,
Selector,
} from '../types/internal';

/**
* Creates `locatorIdSelector` function.
* @internal
*/
export const locatorIdSelectorCreator = (
createSelector: CreateSelector,
getLocatorAttributeName: GetLocatorAttributeNameFn,
): typeof locatorIdSelector => {
): LocatorIdSelector => {
/**
* Selector of locator elements by locator id.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/types/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export type FrozenPartOfTestCafeConfig = DeepReadonly<{
}>;

/**
* The complete pack configuration object without `doBeforePack` property.
* The complete pack configuration object without `doBeforePack` field.
*/
export type FullPackConfigWithoutDoBeforePack<
CustomPackProperties = unknown,
Expand Down Expand Up @@ -85,7 +85,7 @@ export type UserlandPack<
WithDoBeforePack<CustomPackProperties, CustomReportProperties, SkipTests, TestMeta>;

/**
* The complete userland pack config without `doBeforePack` property.
* The complete userland pack config without `doBeforePack` field.
*/
export type UserlandPackWithoutDoBeforePack<
CustomPackProperties = CustomPackPropertiesPlaceholder,
Expand Down
10 changes: 9 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@ export type {
RequestHookResponseEvent,
} from './requestHooks';
export type {ApiRouteClassType, ApiRouteClassTypeWithGetParamsFromUrl} from './routes';
export type {CreateSelectorsOptions, GetLocatorAttributeNameFn, Selector} from './selectors';
export type {
CreateSelector,
CreateSelectorByCss,
CreateSelectorFunctionsOptions,
GetLocatorAttributeNameFn,
LocatorIdSelector,
Selector,
SelectorFunctions,
} from './selectors';
export type {StackFrame} from './stackTrace';
export type {PackageInfo, StartInfo} from './startInfo';
export type {StringForLogs} from './string';
Expand Down
Loading

0 comments on commit 93be1e7

Please sign in to comment.