"use strict";
const expect = require('expect');
const request = require('request');
const execSync = require('child_process').execSync;
const path = require('path');
const fs = require('fs');

describe('CNAME tests', () => {
  it('should support CNAME customization', (done) => {
    // TODO: clean these file changes
    const port = process.env.GITLAB_CE_PAGES_PORT || '80';
    const publicVolume = process.env.GITLAB_CE_PAGES_PUBLIC_VOLUME;
    const cnameVolume = process.env.GITLAB_CE_PAGES_CNAME_VOLUME;
    fs.writeFileSync(path.join(publicVolume, 'groupname/project1', 'index.html'), 'hello1!');
    fs.writeFileSync(path.join(publicVolume, 'groupname/project2', 'index.html'), 'hello2!');
    fs.writeFileSync(path.join(cnameVolume, 'cname.txt'), 'example1.com example2.com groupname/project1');
    setTimeout(() => {
      request.get('http://example1.com' + (port == '80' ? '' : ':' + port), (error, response, body) => {
        expect(!error && body).toEqual('hello1!');
        fs.writeFileSync(path.join(cnameVolume, 'cname.txt'), 'example1.com example2.com groupname/project2');
        setTimeout(() => {
          request.get('http://example2.com' + (port == '80' ? '' : ':' + port), (error, response, body) => {
            expect(!error && body).toEqual('hello2!');
            fs.unlinkSync(path.join(cnameVolume, 'cname.txt'));
            setTimeout(() => {
              request.get('http://example1.com' + (port == '80' ? '' : ':' + port), (error, response, body) => {
                expect(error && response.statusCode).toEqual(404);
                done();
              });
            }, 1000);
          });
        }, 1000);
      });
    }, 1000);
  });
});

@@ -184,16 +184,50 @@
 e('fs');
+%0Aconst mkdirp = require('mkdirp');
 %0A%0Adescri
@@ -345,16 +345,41 @@
 changes%0A
+    // TODO: use promise%0A
     cons
@@ -571,69 +571,460 @@
     
-fs.writeFileSync(path.join(publicVolume, 'groupname/project1'
+const project1Dir = path.join(publicVolume, 'groupname/project1');%0A    const project2Dir = path.join(publicVolume, 'groupname/project2');%0A    mkdirp(project1Dir, (err) =%3E %7B%0A      if (err) %7B%0A        console.log('mkdir', project1Dir, 'failed');%0A        return;%0A      %7D%0A      mkdirp(project2Dir, (err) =%3E %7B%0A        if (err) %7B%0A          console.log('mkdir', project2Dir, 'failed');%0A          return;%0A        %7D%0A        fs.writeFileSync(path.join(project1Dir
 , 'i
@@ -1044,24 +1044,28 @@
 'hello1!');%0A
+    
     fs.write
@@ -1087,41 +1087,16 @@
 oin(
-publicVolume, 'groupname/
 project2
 ', '
@@ -1091,17 +1091,19 @@
 project2
-'
+Dir
 , 'index
@@ -1119,24 +1119,28 @@
 'hello2!');%0A
+    
     fs.write
@@ -1230,24 +1230,28 @@
 project1');%0A
+    
     setTimeo
@@ -1263,24 +1263,28 @@
  =%3E %7B%0A      
+    
 request.get(
@@ -1362,32 +1362,36 @@
 nse, body) =%3E %7B%0A
+    
         expect(!
@@ -1425,32 +1425,36 @@
 lo1!');%0A        
+    
 fs.writeFileSync
@@ -1540,32 +1540,36 @@
 ect2');%0A        
+    
 setTimeout(() =%3E
@@ -1573,32 +1573,36 @@
  =%3E %7B%0A          
+ 
+   
 request.get('htt
@@ -1688,32 +1688,36 @@
 %3E %7B%0A            
+    
 expect(!error &&
@@ -1755,16 +1755,20 @@
         
+ 
+   
 fs.unlin
@@ -1815,32 +1815,36 @@
 ));%0A            
+    
 setTimeout(() =%3E
@@ -1852,32 +1852,36 @@
 %7B%0A              
+  
+  
 request.get('htt
@@ -1975,24 +1975,28 @@
             
+    
 expect(error
@@ -2054,16 +2054,20 @@
     
+  
+  
 done();%0A
     
@@ -2062,16 +2062,64 @@
 done();%0A
+                  %7D);%0A                %7D, 1000);%0A
         
@@ -2193,30 +2193,24 @@
    %7D);%0A    %7D
-, 1000
 );%0A  %7D);%0A%7D);

make group && project folder when not existed during test