Best JavaScript code snippet using stryker-parent
local-etcd.js
Source:local-etcd.js
1var child_process = require('child_process'),2 debug = require('debug')('local-etcd'),3 fs = require('fs'),4 portfinder = require('portfinder'),5 EtcdClient = require('node-etcd');6// https://coreos.com/etcd/docs/latest/docker_guide.html7function promisePort() {8 return new Promise(function(resolve, reject) {9 portfinder.getPort(function (err, port) {10 if (err) {11 reject(err);12 } else {13 resolve(port);14 }15 })16 });17}18function caCertsDir() {19 var caCertsDir = undefined;20 ["/usr/share/ca-certificates"].forEach(function(dir) {21 if (fs.existsSync(dir)) {22 caCertsDir = dir;23 }24 });25 return caCertsDir;26}27/**28 * Run a local etcd in Docker29 *30 * @constructor31 */32var localEtcds = [];33var LocalEtcd = exports.LocalEtcd = function () {34 localEtcds.push(this);35};36LocalEtcd.killAll = function () {37 return Promise.all(localEtcds.map(function (etcd) {38 return etcd.stop();39 }));40};41LocalEtcd.prototype.start = function () {42 var self = this;43 if (self.process) return Promise.resolve();44 var maybeCaCertsMount = [];45 var certsDir = caCertsDir();46 if (certsDir) {47 maybeCaCertsMount = ['-v', certsDir];48 }49 var ip = self.dockerIpAddress();50 var myPromise = Promise.resolve();51 if (!(self.clientPort && self.peerPort)) {52 myPromise = myPromise.then(function () {53 return Promise.all([promisePort(),54 promisePort()]);55 }).then(function (ports) {56 self.peerPort = ports.pop();57 self.clientPort = ports.pop();58 });59 }60 return myPromise.then(function () {61 var uniqueName = 'local-etcd-' + (new Date().getTime());62 var translatedPeerAddress = 'http://' + ip + ':' + String(self.peerPort);63 var translatedClientAddress = 'http://' + ip + ':' + String(self.clientPort);64 return self.runDockerCommandThenWait(Array.prototype.concat(65 ['run'],66 maybeCaCertsMount,67 //68 [69 '-d', '--name', self.containerName(),70 '-p', String(self.clientPort) + ':2379',71 '-p', String(self.peerPort) + ':2380',72 'quay.io/coreos/etcd:v2.0.12',73 '-name', 'etcd0',74 '-listen-client-urls', 'http://0.0.0.0:2379',75 '-advertise-client-urls', translatedClientAddress,76 '-listen-peer-urls', 'http://0.0.0.0:2380',77 '-initial-advertise-peer-urls', translatedPeerAddress,78 '-initial-cluster', 'etcd0=' + translatedPeerAddress,79 '-initial-cluster-token', uniqueName,80 '-initial-cluster-state', 'new'81 ]));82 }).then(function (stdoutAndStderr) {83 var stdoutBuf = stdoutAndStderr[0], stderrBuf = stdoutAndStderr[1];84 process.stderr.write(stderrBuf);85 var matched = String(stdoutBuf).match('^([a-f0-9]{64})\n$');86 if (matched) {87 self.dockerId = matched[1];88 debug("Docker " + self.dockerId + " started");89 } else {90 process.stderr.write(stdoutBuf);91 throw new Error("Unable to start etcd in Docker");92 }93 }).then(function() {94 return new Promise(function (resolve, reject) {95 var logTail = self.runDocker(["logs", "-f", self.dockerId],96 { stdio: ['ignore', process.stdout, 'pipe'] });97 logTail.on("error", reject);98 logTail.on("exit", function () {99 reject(new Error("docker log -f exited prematurely"));100 });101 logTail.stderr.on("data", function (data) {102 var text = String(data).trimRight();103 debug(text);104 if (text.match('became leader')) {105 logTail.kill();106 logTail.removeAllListeners("exit").on("exit", resolve);107 }108 });109 });110 }).then(function() {111 return new Promise(function (resolve, reject) {112 debug("Pinging etcd root");113 self.getClient().get("/", function(err, n) {114 if (err) {115 reject(err);116 } else if (n && n.node && n.node.dir) {117 resolve();118 } else {119 debug("Weird root node: ", n);120 reject(new Error("Weird root node"));121 }122 });123 });124 });125};126LocalEtcd.prototype.stop = function () {127 var self = this;128 if (! self.dockerId) return Promise.resolve();129 return self.runDockerCommandThenWait(["rm", "-f", self.dockerId]);130};131LocalEtcd.prototype.writeTestKeys = function (keys) {132 var client = this.getClient();133 return Promise.all(keys.map(function (kv) {134 var k = kv[0];135 var v = kv[1];136 return new Promise(function (resolve, reject) {137 client.set(k, v, function (err, unused_node) {138 if (err) {139 reject(err);140 } else {141 resolve();142 }143 })144 })145 }));146};147LocalEtcd.prototype.isKitematic = function() {148 return true;149};150LocalEtcd.prototype.containerName = function () { return 'local-etcd'; };151LocalEtcd.prototype.dockerIpAddress = function() {152 if (this.isKitematic()) {153 return this.dockerEnv().DOCKER_HOST.match(/^tcp:\/\/([0-9\.]+)(?:$|:)/)[1];154 } else {155 return '127.0.0.1';156 }157};158LocalEtcd.prototype.getClient = function () {159 return new EtcdClient(this.dockerIpAddress(), this.clientPort);160};161LocalEtcd.prototype.dockerCommand = function () {162 // TODO: portablify163 if (this.isKitematic()) {164 return ("/Applications/Kitematic (Beta).app/Contents/Resources/resources/docker");165 } else {166 throw new Error("Failed at guessing docker path!");167 }168};169LocalEtcd.prototype.dockerEnv = function () {170 // TODO: portablify171 var envCopy = {};172 for (e in process.env) envCopy[e] = process.env[e];173 envCopy.DOCKER_HOST = 'tcp://192.168.99.100:2376';174 envCopy.DOCKER_TLS_VERIFY = 1;175 envCopy.DOCKER_CERT_PATH = '/Users/dom/.docker/machine/machines/dev';176 return envCopy;177};178/**179 * Run a Docker command180 *181 * @param dockerArgs Array of flags and arguments182 * @returns {Promise} An [stdoutBuf, stderrBuf, process] array183 */184LocalEtcd.prototype.runDockerCommandThenWait = function (dockerArgs) {185 var self = this;186 return new Promise(function (resolve, reject) {187 var process = self.runDocker(dockerArgs,188 {stdio: ['ignore', 'pipe', 'pipe']});189 var stdoutBufs = [], stderrBufs = [];190 var todo={stdoutClosed: true, stderrClosed: true, processExited: true};191 function taskDone(feature) {192 delete todo[feature];193 for(var v in todo) return; // return if any to-do items remain194 resolve([Buffer.concat(stdoutBufs), Buffer.concat(stderrBufs),195 process])196 }197 process.on("error", reject);198 process.on("exit", function () {199 taskDone("processExited");200 });201 process.stdout.on("data", function (data) {202 stdoutBufs.push(data);203 });204 process.stdout.on("end", function () {205 taskDone("stdoutClosed");206 });207 process.stderr.on("data", function (data) {208 stderrBufs.push(data);209 });210 process.stderr.on("end", function () {211 taskDone("stderrClosed");212 });213 });214};215LocalEtcd.prototype.runDocker = function (dockerArgs, opt_options) {216 var dockerCommand = this.dockerCommand();217 if (! opt_options) opt_options = {};218 if (! opt_options.env) opt_options.env = this.dockerEnv();219 debug("Running " +220 Array.prototype.concat([dockerCommand], dockerArgs).join(' '));221 return child_process.spawn(dockerCommand, dockerArgs, opt_options);222};223var maybeRedirectStderr = debug.enabled ? "": " 2>/dev/null";224child_process.execSync("'" + LocalEtcd.prototype.dockerCommand() +225 "' rm -f local-etcd" + maybeRedirectStderr + " || true",...
DockerUtils.ts
Source:DockerUtils.ts
1import tty from "tty"2import stream, { Stream } from "stream"3export const collectLogs = (dockerStream: any) => {4 let currentChunk = Buffer.from('');5 let stdOutAndStdErr: Buffer = Buffer.from('');6 const attachStream = new stream.Writable({7 write: function (chunk: Buffer, encoding, next) {8 //header := [8]byte{STREAM_TYPE, 0, 0, 0, SIZE1, SIZE2, SIZE3, SIZE4}9 currentChunk = Buffer.concat([currentChunk, chunk]);10 //const isStdOut = currentChunk.readInt8() === 0x01;11 //const isStdErr = currentChunk.readInt8() === 0x02;12 const payloadSize: number = currentChunk.readUInt32BE(4);13 while (currentChunk.byteLength >= 8 + payloadSize) {14 stdOutAndStdErr = Buffer.concat([stdOutAndStdErr, currentChunk.slice(8, 8 + payloadSize)]);15 currentChunk = currentChunk.slice(8 + payloadSize);16 }17 next();18 },19 });20 21 return new Promise((resolve, reject) => {22 dockerStream.on('end', () => {23 resolve(currentChunk)24 })25 })26}27export class WritableMemoryStream extends stream.Writable {28 buffer: Buffer;29 data : any[] = []30 constructor(){31 super()32 this.buffer = Buffer.from('')33 this.data = []34 this.once('finish', () => {35 this.buffer = this.data.length === 1 ? this.data.pop() : Buffer.concat(this.data)36 })37 }38 _write (chunk : any, enc : string, cb : Function) {39 this.data.push(chunk)40 cb()41 }42}43export const attachStdOut = (stdout: any, dockerStream: any, modem: any, onResize: any) => {44 if (stdout instanceof Array) {45 dockerStream.on('end', function () {46 try {47 stdout[0].end();48 } catch (e) { }49 try {50 stdout[1].end();51 } catch (e) { }52 });53 modem.demuxStream(dockerStream, stdout[0], stdout[1]);54 } else {55 dockerStream.setEncoding('utf8');56 dockerStream.pipe(stdout, {57 end: true58 });59 }60 stdout.on('resize', onResize);61}62export const detachStdout = (stdout: any, onResize: any) => {63 stdout.removeListener('resize', onResize);64}65export const attachStdin = (stdin: tty.ReadStream, dockerStream: any) => {66 stdin.setEncoding('utf8');67 stdin.setRawMode(true);68 stdin.pipe(dockerStream); // stdin -> flow mode69}70export const detachStdin = (stdin: tty.ReadStream, wasRaw: boolean) => {71 stdin.removeAllListeners();72 stdin.setRawMode(wasRaw);73 // The stdin stream is paused by default74 stdin.pause();75 // @ts-ignore76 // https://stackoverflow.com/questions/26004519/why-doesnt-my-node-js-process-terminate-once-all-listeners-have-been-removed/2600475877 // console.log('handles', process._getActiveHandles())78 stdin.unref()...
Using AI Code Generation
1console.log(stdoutAndStderr('Hello World'));2console.log(stdoutAndStderr('Hello World'));3console.log(stdoutAndStderr('Hello World'));4console.log(stdoutAndStderr('Hello World'));5console.log(stdoutAndStderr('Hello World'));6console.log(stdoutAndStderr('Hello World'));7console.log(stdoutAndStderr('Hello World'));8console.log(stdoutAndStderr('Hello World'));9console.log(stdoutAndStderr('Hello World'));10console.log(stdoutAndStderr('Hello World'));11console.log(stdoutAndStderr('Hello World'));12console.log(stdoutAndStderr('Hello World'));13console.log(stdoutAndStderr('Hello World'));14console.log(stdoutAndStderr('Hello World'));15console.log(stdoutAndStderr('Hello World'));16console.log(stdoutAndStderr('Hello World'));17console.log(stdoutAndStderr('Hello World'));18console.log(stdoutAndStderr('Hello World'));19console.log(stdoutAndStderr('Hello World'));
Using AI Code Generation
1var strykerParent = require('stryker-parent');2var childProcess = require('child_process');3var child = childProcess.spawn('node', ['child.js']);4child.stdout.on('data', function (data) {5 console.log('stdout: ' + data);6});7child.stderr.on('data', function (data) {8 console.log('stderr: ' + data);9});10child.on('close', function (code) {11 console.log('child process exited with code ' + code);12});13var strykerParent = require('stryker-parent');14var child = strykerParent.stdoutAndStderr('node', ['child.js']);15child.stdout.on('data', function (data) {16 console.log('stdout: ' + data);17});18child.stderr.on('data', function (data) {19 console.log('stderr: ' + data);20});21child.on('close', function (code) {22 console.log('child process exited with code ' + code);23});24var strykerParent = require('stryker-parent');25var child = strykerParent.stdoutAndStderr('node', ['child.js']);26child.stdout.on('data', function (data) {27 console.log('stdout: ' + data);28});29child.stderr.on('data', function (data) {30 console.log('stderr: ' + data);31});32child.on('close', function (code) {33 console.log('child process exited with code ' + code);34});35var strykerParent = require('stryker-parent');36var child = strykerParent.stdoutAndStderr('node', ['child.js']);37child.stdout.on('data', function (data) {38 console.log('stdout: ' + data);39});40child.stderr.on('data', function (data) {41 console.log('stderr: ' + data);42});43child.on('close', function (code) {44 console.log('child process exited with code ' + code);45});46var strykerParent = require('stryker-parent');
Using AI Code Generation
1var stryker = require('stryker-parent');2var childProcess = require('child_process');3var child = childProcess.spawn('node', ['child.js']);4var stdoutAndStderr = stryker.stdoutAndStderr(child);5stdoutAndStderr.then(function (result) {6 console.log(result);7});8console.log('Hello World');9{ stdout: 'Hello World', stderr: '' }10var stdoutAndStderr = stryker.stdoutAndStderr(child, { timeout: 1000 });11var stdoutAndStderr = stryker.stdoutAndStderr(child, { rejectOnError: false });12var stdoutAndStderr = stryker.stdoutAndStderr(child, { rejectOnTimeout: false });13var stdoutAndStderr = stryker.stdoutAndStderr(child, { rejectOnTimeout: false });
Using AI Code Generation
1const childProcess = require('child_process');2const path = require('path');3const child = childProcess.fork(path.join(__dirname, 'child.js'));4child.on('message', (message) => {5 console.log(message);6});7child.send('Hello from parent');8const { stdoutAndStderr } = require('stryker-child');9process.on('message', (message) => {10 console.log(message);11 stdoutAndStderr.write('Hello from child');12});13process.send('Hello from child');14module.exports = function(config) {15 config.set({16 commandRunner: {17 stdout: (msg) => {18 console.log(msg);19 },20 stderr: (msg) => {21 console.log(msg);22 }23 }24 });25};26const childProcess = require('child_process');27const path = require('path');28const child = childProcess.fork(path.join(__dirname, 'child.js'));29child.on('message', (message) => {30 console.log(message);31});32child.send('Hello from parent');33const { stdoutAndStderr } = require('stryker-child');34process.on('message', (message) => {35 console.log(message);36 stdoutAndStderr.write('Hello from child');37});38process.send('Hello from child');39module.exports = function(config) {40 config.set({41 commandRunner: {42 stdout: (msg) => {43 console.log(msg);44 },45 stderr: (msg) => {
Using AI Code Generation
1var stdoutAndStderr = require('stryker-parent').stdoutAndStderr;2stdoutAndStderr.write('hello');3stdoutAndStderr.write('world');4var stdoutAndStderr = require('stryker-parent').stdoutAndStderr;5stdoutAndStderr.write('hello');6stdoutAndStderr.write('world');7var stdoutAndStderr = require('stryker-parent').stdoutAndStderr;8stdoutAndStderr.write('hello');9stdoutAndStderr.write('world');10var stdoutAndStderr = require('stryker-parent').stdoutAndStderr;11stdoutAndStderr.write('hello');12stdoutAndStderr.write('world');13var stdoutAndStderr = require('stryker-parent').stdoutAndStderr;14stdoutAndStderr.write('hello');15stdoutAndStderr.write('world');16var stdoutAndStderr = require('stryker-parent').stdoutAndStderr;17stdoutAndStderr.write('hello');18stdoutAndStderr.write('world');19var stdoutAndStderr = require('stryker-parent').stdoutAndStderr;20stdoutAndStderr.write('hello');21stdoutAndStderr.write('world');22var stdoutAndStderr = require('stryker-parent').stdoutAndStderr;23stdoutAndStderr.write('hello');24stdoutAndStderr.write('world');25var stdoutAndStderr = require('stryker-parent').stdoutAndStderr;26stdoutAndStderr.write('hello');27stdoutAndStderr.write('world');28var stdoutAndStderr = require('stryker-parent').stdout
Using AI Code Generation
1const { stdoutAndStderr } = require('stryker-parent');2console.log('Hello world');3stdoutAndStderr('Hello world');4import { stdoutAndStderr } from 'stryker-parent';5console.log('Hello world');6stdoutAndStderr('Hello world');7Copyright (c) 2019 Stryker Mutator
Using AI Code Generation
1const childProcess = require('child_process');2const { stdoutAndStderr } = require('stryker-parent');3const child = childProcess.fork('./child.js');4child.on('message', (message) => {5 if (message === 'start') {6 stdoutAndStderr('Starting child process');7 child.send('start');8 }9});10child.on('exit', (code, signal) => {11 console.log('Child process exited with code ' + code + ' and signal ' + signal);12});13const { stdoutAndStderr } = require('stryker-parent');14process.on('message', (message) => {15 if (message === 'start') {16 stdoutAndStderr('Child process started');17 process.send('started');18 }19});20module.exports = function (config) {21 config.set({22 commandRunner: {23 },24 });25};
Using AI Code Generation
1var stryker = require('stryker-parent');2var stdoutAndStderr = stryker.stdoutAndStderr;3stdoutAndStderr('Hello World');4var stryker = require('stryker-parent');5var stdoutAndStderr = stryker.stdoutAndStderr;6stdoutAndStderr('Hello World');7var stryker = require('stryker-parent');8var stdoutAndStderr = stryker.stdoutAndStderr;9stdoutAndStderr('Hello World');10var stryker = require('stryker-parent');11var stdoutAndStderr = stryker.stdoutAndStderr;12stdoutAndStderr('Hello World');13var stryker = require('stryker-parent');14var stdoutAndStderr = stryker.stdoutAndStderr;15stdoutAndStderr('Hello World');16var stryker = require('stryker-parent');17var stdoutAndStderr = stryker.stdoutAndStderr;18stdoutAndStderr('Hello World');19var stryker = require('stryker-parent');20var stdoutAndStderr = stryker.stdoutAndStderr;21stdoutAndStderr('Hello World');22var stryker = require('stryker-parent');23var stdoutAndStderr = stryker.stdoutAndStderr;24stdoutAndStderr('Hello World');25var stryker = require('stryker-parent');
Using AI Code Generation
1var stryker = require('stryker-parent').stryker;2var options = {3};4stryker(stdoutAndStderr, options);5module.exports = function(config) {6 config.set({7 preprocessors: {8 },9 coverageReporter: {10 },11 });12};
Using AI Code Generation
1var stryker = require('stryker-parent');2var test = stryker.stdoutAndStderr(function () {3 console.log("Hello World");4});5console.log(test);6module.exports.stdoutAndStderr = function (fn) {7 buf = '';8 process.stdout.write = process.stderr.write = function (str) {9 buf += str;10 };11 fn();12 process.stdout.write = oldWrite;13 process.stderr.write = oldErr;14 return buf;15};
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!