Use travis for build and testing with electron - node.js

I have an electron project with spectron tests. I use travis to build for every major OS with electron-builder. Now, I want to also run tests in travis, so I updated my .travis.yml file to run tests on linux (for headless tests) and build on osx. This works, but only the build part on osx works, while the tests fails.
This is my last try of travis config
language: node_js
matrix:
include:
- os: linux
- os: osx
osx_image: xcode10.2
node_js: '12'
addons:
chrome: stable
apt:
packages:
- xvfb
script:
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then xvfb-run test; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then npm run dist; fi
This fails without any valuable information
I have also tried this travis config
addons:
apt:
packages:
- xvfb
before_script:
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then export DISPLAY=:99.0; fi
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sh -e /etc/init.d/xvfb start; fi
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sleep 3; fi
script:
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then npm test; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then npm run dist; fi
that one fails because of timeout after 10 minutes. So I have no clue here of what it is going on. How can I tests my electron app in travis while also keeping the build on osx? Is there anything wrong on my travis config?
PD: Here is the repo where I have my project

Is it possible that you want to call
xvfb-run npm test
instead of
xvfb-run test
?
Otherwise, xvfb-run test will call the test command (reference) which returns 1 because there's no expression given i.e. expression evaluates to false.

Related

Github desktop cannot commit with precommit hook

I am using win10.
when clicking the blue button on the Github desktop to commit something.
error husky - pre-commit hook exited with code 1 (error) appears.
But if i type git commit -m "sth" in terminal, the pre-commit hook works fine
=============================
part of my package.json
"lint-staged": {
"*.{js,jsx,tsx,ts}": "eslint --cache --fix",
"*.{js,css,md,jsx,tsx,ts}": "prettier --write"
}
============================
.husky/pre-commit.sh
. "$(dirname "$0")/_/husky.sh"
npx lint-staged
============================
.husky/_/husky.sh
#!/bin/sh
if [ -z "$husky_skip_init" ]; then
debug () {
if [ "$HUSKY_DEBUG" = "1" ]; then
echo "husky (debug) - $1"
fi
}
readonly hook_name="$(basename "$0")"
debug "starting $hook_name..."
if [ "$HUSKY" = "0" ]; then
debug "HUSKY env variable is set to 0, skipping hook"
exit 0
fi
if [ -f ~/.huskyrc ]; then
debug "sourcing ~/.huskyrc"
. ~/.huskyrc
fi
export readonly husky_skip_init=1
sh -e "$0" "$#"
exitCode="$?"
if [ $exitCode != 0 ]; then
echo "husky - $hook_name hook exited with code $exitCode (error)"
fi
exit $exitCode
fi
The issue (even though it's not a real issue! ) is because of the hooks created by Husky. Husky is an npm package that lets you define npm scripts that correlate to local Git events such as a commit or push. And this helps in enforcing collaborative standards in a project. The quick solution, if you are too busy, is to simply delete the hooks folder for git which defines the pre-commit hooks and hence can push after that. (This is just kind of a hack to avoid editing thousands of files at a time for lint errors. Follow the guidelines and resolve all the lint errors for better code quality and maintainability. ) But it's always better to understand how Husky and hooks work and properly follow the lint warnings.
Edit: You can also skip hooks when you provide the git command line argument —no-verify,
git push origin master --no-verify
, or use Sourcetree‘s Bypass commit hooks setting (in the menu to the top right of the commit message field)

Circleci create environment variable from existing env var

In the circle ci dashboard I set enviornment variables like DEV_USEFUl_API_KEY, PROD_USEFUL_API_KEY. Then in my circleci config file (v2.1) I do this
- run:
name: Run Tests
command: |
if [ "${CIRCLE_BRANCH}" == "master" ]; then
echo 'export FIREBASE_API_KEY=${PROD_FIREBASE_API_KEY}' >> $BASH_ENV
elif [[ "${CIRCLE_BRANCH}" == "develop" ]]; then
echo 'export FIREBASE_API_KEY=${DEV_FIREBASE_API_KEY}' >> $BASH_ENV
fi
yarn test
The idea been when my tests are run, the environment variable will be read. I have printed out within my node application process.env and I can see the variables PROD_FIREBASE_API_KEY, DEV_FIREBASE_API_KEY are within the environment, however there is no FIREBASE_API_KEY as intended.
I have tried chaning the syntax of the command to:
echo 'export FIREBASE_API_KEY=$PROD_FIREBASE_API_KEY' >> $BASH_ENV
and also
echo 'export FIREBASE_API_KEY="$PROD_FIREBASE_API_KEY"' >> $BASH_ENV
However its not making any difference, the variable FIREBASE_API_KEY is not set, can anyone please advise? Thanks.
The solution was to put yarn test in another run step, as its necessary for the bash profile to be reloaded after setting new env vars (which happens at the beginning of every run).
- run:
name: Configure Environment Variables
command: |
if [ "${CIRCLE_BRANCH}" == "master" ]; then
# Set env variables
elif [[ "${CIRCLE_BRANCH}" == "develop" ]]; then
# Set env variables
fi
- run:
name: Run Tests
command: |
yarn test

Cucumber tests fail but travis build still passes

I am using Travis for CI. For some reason, the builds pass even when some tests fail. See the full log here
https://travis-ci.org/msm1089/hobnob/jobs/534173396
The way I am running the tests is via a bash script, e2e.test.sh, that is run by yarn.
Searching for this specific issue has not turned up anything that helps. It is something to do with exit codes I believe. I think I need to somehow get the build to exit with non-zero, but as you can see at bottom of the log, yarn exits with 0.
e2e.test.sh
#!/usr/bin/env bash
RETRY_INTERVAL=${RETRY_INTERVAL:-0.2}
# Run our API server as a background process
if [[ "$OSTYPE" == "msys" ]]; then
if ! netstat -aon | grep "0.0.0.0:$SERVER_PORT" | grep "LISTENING"; then
pm2 start --no-autorestart --name test:serve "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js" -- run test:serve
until netstat -aon | grep "0.0.0.0:$SERVER_PORT" | grep "LISTENING"; do
sleep $RETRY_INTERVAL
done
fi
else
if ! ss -lnt | grep -q :$SERVER_PORT; then
yarn run test:serve &
fi
until ss -lnt | grep -q :$SERVER_PORT; do
sleep $RETRY_INTERVAL
done
fi
npx cucumber-js spec/cucumber/features --require-module #babel/register --require spec/cucumber/steps
if [[ "$OSTYPE" == "msys" ]]; then
pm2 delete test:serve
fi
travis.yml
language: node_js
node_js:
- 'node'
- 'lts/*'
- '10'
- '10.15.3'
services:
- elasticsearch
before_install:
- curl -O https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.6.1.deb
- sudo dpkg -i --force-confnew elasticsearch-6.6.1.deb
- sudo service elasticsearch restart
before_script:
- sleep 10
env:
global:
- NODE_ENV=test
- SERVER_PROTOCOL=http
- SERVER_HOSTNAME=localhost
- SERVER_PORT=8888
- ELASTICSEARCH_PROTOCOL=http
- ELASTICSEARCH_HOSTNAME=localhost
- ELASTICSEARCH_PORT=9200
- ELASTICSEARCH_INDEX=test
package.json
...
scripts:{
"test": "yarn run test:unit && yarn run test:integration && yarn run test:e2e"
}
...
So, how can I ensure that the cucumber exit code is the one that is returned, so that the build fails as it should when the tests don't pass?
There are a few possible ways to solve this. Here are two of my favorite.
Option 1:
Add set -e at the top of your bash script, so that it exits on first error, preserving the exit code, and subsequently, failing Travis if its a non zero.
Option 2:
Capture whatever exit code you want, and exit with it wherever it makes sense.
run whatever command here
exitcode=$?
[[ $exitcode == 0 ]] || exit $exitcode
As a side note - it seems like your bash script has too many responsibilities. I would consider separating them if possible, and then you give travis a list of commands to run, and possibly one or two before_script commands.
Something along these lines:
# .travis.yml
before_script:
- ./start_server.sh
script:
- npx cucumber-js spec/cucumber/features ...

How to detect whether Node installed when node is not available in Environment

I am trying to package an app that I have built for the Mac OS using the Packages software. For the app to install, there are pre-requisites which should be already installed on the system, one of which is node. I am checking for the pre requisites by Defining Requirement based on the result of an external shell script.
Basically, the packager software runs the specified external script and if the script returns a given value it proceeds else it throws an error.
I have written the following script to detect whether node is installed
#! /bin/sh
echo "Checking PreReq Node"
node --version | grep "v" &> /dev/null
if [ $? == 0 ]; then
echo "Node Installed"
exit 0;
else
echo "Node not installed"
exit 1;
fi
This works as expected when I run it in the shell, but when running within the context of the installer, node is not available in the environment so it fails. If I change the script to use the full path of node it works
#! /bin/sh
echo "Checking PreReq Node"
/usr/local/bin/node --version | grep "v" &> /dev/null
if [ $? == 0 ]; then
echo "Node Installed"
exit 0;
else
echo "Node not installed"
exit 1;
fi
However, node may be installed in a different location on a different system.
How can I check whether node is installed on a system without actually running node?
This may help you:
whereis node | grep ' ' -ic

Not being able to run bash script on windows

I am trying hard to run a bash script on windows but the message I am getting is "Sorry, only Linux and MacOS are supported."
I have installed Cygwin and Clink in order to be able to run sh scripts on windows platform but still it is of no avail.
Here is my bash script,
#!/bin/bash
for ((j=0;j<10;j++)); do
rtg map -i sample_NA19240/SRR003988 -t hg19 -o map_sample_NA19240/SRR003988- $j --start-read=$[j*1000000] --end-read=$[(j+1)*1000000]
done
It's in the program you are using, "rtg" :
# Pre-flight safety-belts
if [ "$(uname -s)" != "Linux" ] && [ "$(uname -s)" != "Darwin" ]; then
# If you comment this check out you are on your own :-)
echo "Sorry, only Linux and MacOS are supported."
exit 1
You can try the "suggestion", that is, remove the check in the file installer/rtg. If it works, you are lucky. Else use a vm or ask the rtg author.

Resources