Skip to content

Commit

Permalink
chore: refactor testing code
Browse files Browse the repository at this point in the history
Reshuffles the testing code to consolidate all CI-related setup into a
single setup file.  Removes the PDF comparison test since the output of
wkhtmltopdf is not deterministic [1].

[1] wkhtmltopdf/wkhtmltopdf#4437
  • Loading branch information
jniles committed Aug 15, 2019
1 parent 0357ec2 commit 64b3c96
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 117 deletions.
5 changes: 2 additions & 3 deletions .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ image:
stack: node 10, mysql, redis

install:
- yarn
- bash ./sh/setup-ci-env.sh
- bash ./sh/appveyor.sh
- sudo service mysql restart
- yarn

build: off

Expand All @@ -24,5 +24,4 @@ environment:
test_script:
- yarn build
- yarn build:db
- mkdir -p test/artifacts
- yarn test:server-unit
8 changes: 5 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ dist: trusty

before_install:
- jdk_switcher use oraclejdk8
- ./sh/setup-ci-env.sh
- ./sh/travis.sh

services:
Expand All @@ -14,14 +15,15 @@ node_js:
- lts/dubnium

before_script:
- sudo /etc/init.d/mysql restart
- yarn build
- yarn webdriver-manager update --versions.chrome="$(google-chrome --version | cut -d ' ' -f 3)" --gecko false
- greenkeeper-lockfile-update

script:
- mkdir test/artifacts
- yarn test
- yarn test:client-unit
- yarn test:integration
- yarn test:ends


after_script: greenkeeper-lockfile-upload

Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
"app": "cross-env NODE_ENV=production gulp build && cd bin && cross-env NODE_ENV=production node server/app.js",
"dev": "cross-env NODE_ENV=development gulp build && cd bin && cross-env NODE_ENV=development node server/app.js",
"test": "./sh/test.sh",
"test:integration": "./sh/integration-tests.sh",
"test:ends": "./sh/test-ends.sh",
"test:client-unit": "./node_modules/.bin/karma start --single-run --no-auto-watch --concurrency 1 karma.conf.js",
"test:server-unit": "./node_modules/.bin/mocha --recursive --exit test/server-unit",
"test:integration": "SUITE=\"integration\" ./sh/test.sh",
"test:ends": "SUITE=\"end-to-end\" ./sh/tesh.sh",
"test:client-unit": "SUITE=\"client-unit\" ./sh/test.sh",
"test:server-unit": "SUITE=\"server-unit\" ./sh/test.sh",
"build": "./node_modules/.bin/gulp build",
"build:db": "./sh/build-database.sh",
"build:clean": "./sh/build-init-database.sh",
Expand Down
2 changes: 1 addition & 1 deletion server/controllers/medical/reports/visits.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</div>
</header>
<br>

<!-- patient information -->
<span><i class="fa fa-caret-right"></i> {{translate 'PATIENT_RECORDS.VISITS.PATIENT_INFO'}}</span>
<div class="row" style="border: 1px solid #ccc; padding: 10px; margin-bottom: 15px;">
Expand Down
23 changes: 0 additions & 23 deletions sh/appveyor.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,23 +1,5 @@
#!/usr/bin/env bash

##
## This script is for Appveyor CI to configure and install required software.
##

## mysql is installed by default on appveyor, so we can set mysql server charset as utf8mb4
echo -e "
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
collation-server=utf8mb4_unicode_ci
init-connect='SET NAMES utf8mb4'
character-set-server=utf8mb4
sql-mode=\"STRICT_ALL_TABLES\" " | sudo tee -a /etc/mysql/conf.d/99_custom_travis.cnf

## set database environment
set -euo pipefail

Expand All @@ -30,8 +12,3 @@ ROOT_DB_PASS="Password12!"
# setup usernames and permissions
mysql -h $DB_HOST -u root -p$ROOT_DB_PASS -e "GRANT ALL PRIVILEGES ON *.* TO '$DB_USER'@'$DB_HOST' IDENTIFIED BY '$DB_PASS' WITH GRANT OPTION;"
mysql -h $DB_HOST -u root -p$ROOT_DB_PASS -e "FLUSH PRIVILEGES;"

# download and install wkhtmltopdf globally
wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.4/wkhtmltox-0.12.4_linux-generic-amd64.tar.xz
tar -xf wkhtmltox-0.12.4_linux-generic-amd64.tar.xz
sudo mv ./wkhtmltox/bin/wkhtmltopdf /usr/bin/wkhtmltopdf
37 changes: 37 additions & 0 deletions sh/setup-ci-env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env bash

##
## This script is for both Appveyor and Travis to configure and install required software to
## get BHIMA up and running.
##

## mysql is installed by default on appveyor, so we can set mysql server charset as utf8mb4
echo -e "
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
collation-server=utf8mb4_unicode_ci
init-connect='SET NAMES utf8mb4'
character-set-server=utf8mb4
sql-mode=\"STRICT_ALL_TABLES\" " | sudo tee -a /etc/mysql/conf.d/99_custom_ci.cnf

## set database environment
set -euo pipefail

set -a
source .env.development
set +a

# download and install wkhtmltopdf globally
wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.4/wkhtmltox-0.12.4_linux-generic-amd64.tar.xz
tar -xf wkhtmltox-0.12.4_linux-generic-amd64.tar.xz
sudo mv ./wkhtmltox/bin/wkhtmltopdf /usr/bin/wkhtmltopdf

mkdir -p test/artifacts

# restart mysql once chanes have been made
sudo service mysql restart
38 changes: 24 additions & 14 deletions sh/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,34 @@ function endfold {
fi
}

SUITE=${SUITE:-"ALL"}

# run karma (client unit) tests
startfold "Running Client Unit Tests..." "test-client-unit";
./node_modules/.bin/karma start --single-run --no-auto-watch --concurrency 1 karma.conf.js
endfold "test-client-unit" ;
if [ $SUITE = "client-unit" ] || [ $SUITE = "ALL" ] ; then
startfold "Running Client Unit Tests..." "test-client-unit";
./node_modules/.bin/karma start --single-run --no-auto-watch --concurrency 1 karma.conf.js
endfold "test-client-unit" ;
fi

# run integration tests
startfold "Running Integration Tests..." "test-integration";
./sh/integration-tests.sh
endfold "test-integration" ;

#run server-unit test
# startfold "Running server Unit Tests ......" "server-unit"
# ./node_modules/.bin/mocha --recursive --exit test/server-unit
# endfold "server-unit" ;
if [ $SUITE = "integration" ] || [ $SUITE = "ALL" ] ; then
startfold "Running Integration Tests..." "test-integration";
./sh/integration-tests.sh
endfold "test-integration" ;
fi

# run server-unit test
if [ $SUITE = "server-unit" ] || [ $SUITE = "ALL" ] ; then
startfold "Running server Unit Tests ......" "server-unit"
./node_modules/.bin/mocha --recursive --exit test/server-unit
endfold "server-unit" ;
fi

# run end to end tests
startfold "Running Client End to End Tests..." "test-end-to-end";
./sh/test-ends.sh
endfold "test-end-to-end" ;
if [ $SUITE = "end-to-end" ] || [ $SUITE = "ALL" ] ; then
startfold "Running Client End to End Tests..." "test-end-to-end";
./sh/test-ends.sh
endfold "test-end-to-end" ;
fi

exit 0;
23 changes: 0 additions & 23 deletions sh/travis.sh
Original file line number Diff line number Diff line change
@@ -1,23 +1,5 @@
#!/usr/bin/env bash

##
## This script is for Travis CI to configure and install required software.
##

## mysql is installed by default on travis, so we can set mysql server charset as utf8mb4
echo -e "
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
collation-server=utf8mb4_unicode_ci
init-connect='SET NAMES utf8mb4'
character-set-server=utf8mb4
sql-mode=\"STRICT_ALL_TABLES\" " | sudo tee -a /etc/mysql/conf.d/99_custom_travis.cnf

## set database environment
set -euo pipefail

Expand All @@ -29,11 +11,6 @@ set +a
mysql -h $DB_HOST -u root -e "GRANT ALL PRIVILEGES ON *.* TO '$DB_USER'@'$DB_HOST' IDENTIFIED BY '$DB_PASS' WITH GRANT OPTION;"
mysql -h $DB_HOST -u root -e "FLUSH PRIVILEGES;"

# download and install wkhtmltopdf globally
wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.4/wkhtmltox-0.12.4_linux-generic-amd64.tar.xz
tar -xf wkhtmltox-0.12.4_linux-generic-amd64.tar.xz
sudo mv ./wkhtmltox/bin/wkhtmltopdf /usr/bin/wkhtmltopdf

# install greenkeeper lockfile
yarn global add greenkeeper-lockfile@1
export GK_LOCK_YARN_OPTS="--ignore-engines"
Binary file removed test/fixtures/pdf-sample.pdf
Binary file not shown.
46 changes: 0 additions & 46 deletions test/server-unit/pdf.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ const random = Math.ceil(Math.random() * (10 ** 9));
const fixturesPath = path.resolve('test/fixtures');
const artifactsPath = path.resolve('test/artifacts');
const htmlFile = path.join(fixturesPath, '/pdf-sample.html');
const pdfFile = path.join(fixturesPath, '/pdf-sample.pdf');
const temporaryFile = path.join(artifactsPath, `/pdf-${random}.pdf`);

function PDFRenderUnitTest() {
Expand All @@ -53,24 +52,6 @@ function PDFRenderUnitTest() {
expect(isBuffer && hasValidVersion).to.be.equal(true);
});

it.skip('#pdf.render() renders an identical PDF given an HTML template', async () => {
// load the HTML template into memory as a giant string
const tmpl = await fs.readFile(htmlFile, 'utf8');

// give the giant string to the render method
const rendered = await pdf.render({}, tmpl, {});
const cached = await fs.readFile(pdfFile);

expect(isBufferInstance(rendered)).to.equal(true);
expect(isBufferInstance(cached)).to.equal(true);

// pdf DateCreation must be ignored when comparing
const slicedRendered = sliceOutCreationDate(rendered);
const slicedCached = sliceOutCreationDate(cached);

expect(sliceOutRandomMetadata(slicedRendered)).to.deep.equal(sliceOutRandomMetadata(slicedCached));
});

it('#pdf.render() templates in a barcode to the pdf file', async () => {
const tmpl = await fs.readFile(path.join(fixturesPath, templateWithBarcode), 'utf8');
const params = { main : 'This is a test', value : 'hi' };
Expand All @@ -81,18 +62,6 @@ function PDFRenderUnitTest() {
});
}

/**
* sliceOutCreationDate
* @description remove the CreationDate from the PDF buffer
* @param {buffer} buffer
*/
function sliceOutCreationDate(buffer) {
const start = buffer.indexOf('/CreationDate');
const end = buffer.indexOf(')', start) + 1;
const firstPart = buffer.slice(0, start);
const secondPart = buffer.slice(end);
return Buffer.concat([firstPart, secondPart]);
}

/**
* hasValidPdfVersion
Expand All @@ -106,21 +75,6 @@ function hasValidPdfVersion(fileInString) {
return !!(result.length);
}

/**
* @function sliceOutRandomMetadata
* @description
* Slices out random binary data at the end of the PDF.
*
* @param {Buffer} buffer
*/
function sliceOutRandomMetadata(buffer) {
const start = buffer.indexOf('\nxref\n0 17');
const end = buffer.indexOf('%%EOF');
const firstPart = buffer.slice(0, start);
const secondPart = buffer.slice(end);
return Buffer.concat([firstPart, secondPart]);
}

/**
* @description check if the given file is an instance of Buffer
* @param {object} file
Expand Down

0 comments on commit 64b3c96

Please sign in to comment.