11var fs = require ( 'fs' ) ;
2+ var path = require ( 'path' ) ;
23var exec = require ( 'child_process' ) . exec ;
4+ var execSync = require ( 'child_process' ) . execSync ;
5+ var constants = require ( './constants' ) ;
36
47exports . execCmd = function ( cmd , cb , errorCb ) {
58 cb = cb ? cb : function ( ) { } ;
@@ -22,8 +25,7 @@ exports.writeFile = function(filePath, content, cb) {
2225exports . doesDirExist = function ( dirPath ) {
2326 try {
2427 if ( fs . statSync ( dirPath ) . isDirectory ( ) ) return true ;
25- }
26- catch ( e ) {
28+ } catch ( e ) {
2729 return false ;
2830 }
2931
@@ -33,8 +35,7 @@ exports.doesDirExist = function(dirPath) {
3335exports . doesFileExist = function ( filePath ) {
3436 try {
3537 if ( fs . statSync ( filePath ) . isFile ( ) ) return true ;
36- }
37- catch ( e ) {
38+ } catch ( e ) {
3839 return false ;
3940 }
4041
@@ -53,8 +54,8 @@ exports.getTimeLastModified = function(filePath) {
5354 throw new Error ( filePath + ' does not exist' ) ;
5455 }
5556
56- var stats = fs . statSync ( filePath ) ,
57- formattedTime = exports . formatTime ( stats . mtime ) ;
57+ var stats = fs . statSync ( filePath ) ;
58+ var formattedTime = exports . formatTime ( stats . mtime ) ;
5859
5960 return formattedTime ;
6061} ;
@@ -66,3 +67,55 @@ exports.touch = function(filePath) {
6667exports . throwOnError = function ( err ) {
6768 if ( err ) throw err ;
6869} ;
70+
71+ exports . testImageWrapper = function ( opts ) {
72+ var isCI = process . env . CIRCLECI ;
73+ var useLocalElectron = process . env . LOCAL_ELECTRON ;
74+ var args = opts . args . join ( ' ' ) ;
75+
76+ var msg = [
77+ 'Running ' + opts . msg + ' using build/plotly.js from' ,
78+ exports . getTimeLastModified ( constants . pathToPlotlyBuild ) ,
79+ '\n'
80+ ] . join ( ' ' ) ;
81+
82+ var pathToElectron ;
83+ var pathToScript ;
84+ var cmd ;
85+
86+ if ( useLocalElectron ) {
87+ try {
88+ // N.B. this is what require('electron') in a node context
89+ pathToElectron = require ( 'electron' ) ;
90+ } catch ( e ) {
91+ throw new Error ( 'electron not installed locally' ) ;
92+ }
93+
94+ pathToScript = path . join ( constants . pathToImageTest , opts . script ) ;
95+ cmd = [ pathToElectron , pathToScript , args ] . join ( ' ' ) ;
96+ } else {
97+ pathToElectron = path . join ( constants . testContainerHome , '..' , 'node_modules' , '.bin' , 'electron' ) ;
98+ pathToScript = path . join ( constants . testContainerHome , 'test' , 'image' , opts . script ) ;
99+
100+ var baseCmd = [
101+ 'xvfb-run --auto-servernum' ,
102+ '--server-args \'-screen 0, 1024x768x24\'' ,
103+ pathToElectron , pathToScript , args
104+ ] . join ( ' ' ) ;
105+
106+ if ( isCI ) {
107+ cmd = baseCmd ;
108+ } else {
109+ cmd = [
110+ 'docker exec -i' , constants . testContainerName ,
111+ '/bin/bash -c' ,
112+ '"' + baseCmd + '"'
113+ ] . join ( ' ' ) ;
114+ }
115+ }
116+
117+ console . log ( msg ) ;
118+ if ( process . env . DEBUG ) console . log ( '\n' + cmd ) ;
119+
120+ execSync ( cmd , { stdio : [ 0 , 1 , 2 ] } ) ;
121+ } ;
0 commit comments