import fs from 'fs';
import path from 'path';
import childProcess from 'child_process';
import promisify from 'es6-promisify';

const exec = promisify(childProcess.exec);

const exit = (...messages) => {
  // eslint-disable-next-line
  console.error(...messages);
  process.exit(1);
};

jest.setTimeout(60000);

describe('compiled cpp validity', () => {
  try {
    fs.readdirSync(__dirname).forEach((file) => {
      if (/\.cpp$/.test(file)) {
        test(file, (done) => {
          exec(`emcc --bind -s \"EXPORTED_RUNTIME_METHODS=['UTF8ToString']\" -s AGGRESSIVE_VARIABLE_ELIMINATION=1 -s ELIMINATE_DUPLICATE_FUNCTIONS=1 -s ABORTING_MALLOC=1 -s NO_EXIT_RUNTIME=1 -s NO_FILESYSTEM=1 -s DISABLE_EXCEPTION_CATCHING=2 ${path.join(__dirname, file)} ./node_modules/asm-dom/cpp/asm-dom.cpp -o ./temp/${file.replace(/\.cpp$/, '')}.asm.js`)
            .then(done)
            .catch((emccError) => {
              exec(`exec error: ${emccError}`);
            });
        });
      }
    });
  } catch (ex) {
    exit('Could not list the directory.', ex);
  }
});

@@ -455,13 +455,145 @@
 est(
-file,
+%0A          file%0A            .replace(/%5C.cpp$/, '')%0A            .replace(/(%5Ba-z%5D)(%5BA-Z%5D)/g, '$1 $2')%0A            .toLowerCase(),%0A         
  (do
@@ -597,24 +597,26 @@
 (done) =%3E %7B%0A
+  
           ex
@@ -975,16 +975,18 @@
         
+  
 .then(do
@@ -989,16 +989,18 @@
 n(done)%0A
+  
         
@@ -1037,24 +1037,26 @@
             
+ 
+ 
 exec(%60exec e
@@ -1085,28 +1085,32 @@
             
+  
 %7D);%0A
+  
         %7D);%0A
@@ -1098,32 +1098,42 @@
  %7D);%0A          %7D
+,%0A        
 );%0A      %7D%0A    %7D

improve cpp test name