'use strict';

const os = require('os');
const fs = require('fs');
const path = require('path');
const childProcess = require('child_process');

const { describe, it } = require('mocha');

function exec(command, options = {}) {
  return childProcess.execSync(command, {
    stdio: 'inherit',
    ...options,
  });
}

describe('Integration Tests', () => {
  const tmpDir = path.join(os.tmpdir(), 'graphql-js-integrationTmp');
  fs.rmdirSync(tmpDir, { recursive: true });
  fs.mkdirSync(tmpDir);

  const distDir = path.resolve('./npmDist');
  exec(`npm pack ${distDir} && cp graphql-*.tgz graphql.tgz`, { cwd: tmpDir });

  function testOnNodeProject(projectName) {
    exec(`cp -R ${path.join(__dirname, projectName)} ${tmpDir}`);

    const cwd = path.join(tmpDir, projectName);
    exec('npm install --quiet', { cwd });
    exec('npm test', { cwd });
  }

  it('Should compile with all supported TS versions', () => {
    testOnNodeProject('ts');
  }).timeout(40000);

  it('Should work on all supported node versions', () => {
    testOnNodeProject('node');
  }).timeout(40000);
});

@@ -227,14 +227,22 @@
 %7B%0A  
-return
+const result =
  chi
@@ -279,23 +279,24 @@
     
-stdio: 'inherit
+encoding: 'utf-8
 ',%0A 
@@ -316,16 +316,69 @@
 ,%0A  %7D);%0A
+  return result != null ? result.trimEnd() : result;%0A
 %7D%0A%0Adescr
@@ -596,16 +596,36 @@
 ist');%0A 
+ const archiveName =
  exec(%60n
@@ -627,16 +627,24 @@
 ec(%60npm 
+--quiet 
 pack $%7Bd
@@ -654,58 +654,122 @@
 Dir%7D
- && cp graphql-*.tgz graphql.tgz%60, %7B cwd: tmpDir %7D
+%60, %7B cwd: tmpDir %7D);%0A  fs.renameSync(%0A    path.join(tmpDir, archiveName),%0A    path.join(tmpDir, 'graphql.tgz'),%0A  
 );%0A%0A
@@ -945,31 +945,49 @@
 npm 
-install --quiet', %7B cwd
+--quiet install', %7B cwd, stdio: 'inherit'
  %7D);
@@ -1001,16 +1001,24 @@
 ec('npm 
+--quiet 
 test', %7B
@@ -1021,16 +1021,34 @@
 ', %7B cwd
+, stdio: 'inherit'
  %7D);%0A  %7D

remove shell command for copying files (#2802)