File size: 3,569 Bytes
bc20498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const buildOutputWrapper = require('../src');
const tmp = require('tmp');
const fs = require('fs');
const { expect } = require('chai');
const path = require('path');

describe('output-wrapper', function() {
  let output, temp, node;

  beforeEach(() => {
    temp = tmp.dirSync();
    node = {
      outputPath: temp.name,
    };
    output = buildOutputWrapper(node);
  });

  it('should write to given location', function() {
    output.writeFileSync('test.md', 'test');
    let content = fs.readFileSync(`${temp.name}/test.md`, 'UTF-8');
    expect(content).to.be.equal('test');
  });

  it(`takes absolute path as input`, function() {
    output.writeFileSync('test.md', 'test');
    expect(output.existsSync(`${temp.name}/test.md`, 'test')).to.be.true;
  });

  it(`throws actionable error when relative path is traverses above outputPath`, function() {
    expect(() => output.writeFileSync(`../../../test.md`, 'test')).to.throw(
      `Traversing above the outputPath is not allowed. Relative path ../../../test.md traverses beyond ${node.outputPath}`
    );
  });

  it(`should not allow other fs operations`, function() {
    expect(() => output.writevSync('test.md', 'test')).to.throw(
      /^Operation writevSync is not allowed to use. Allowed operations are readFileSync,existsSync,lstatSync,readdirSync,statSync,writeFileSync,appendFileSync,rmdirSync,mkdirSync,unlinkSync,symlinkOrCopySync,symlinkSync,utimesSync,outputFileSync$/
    );
  });

  it('can create folder recursively', function() {
    let SUBFOLDER_PATH = 'main/sub';
    expect(fs.existsSync(`${temp.name}/${SUBFOLDER_PATH}`)).to.be.false;
    output.mkdirSync(SUBFOLDER_PATH, { recursive: true });
    expect(fs.existsSync(`${temp.name}/${SUBFOLDER_PATH}`)).to.be.true;
  });

  it('can create folder non recursive', function() {
    expect(fs.existsSync(`${temp.name}/test`)).to.be.false;
    output.mkdirSync('test');
    expect(fs.existsSync(`${temp.name}/test`)).to.be.true;
  });

  it('can remove folder recursively', function() {
    output.mkdirSync('test');
    expect(fs.existsSync(`${temp.name}/test`)).to.be.true;
    output.writeFileSync('test.md', 'test');
    output.rmdirSync('./', {
      recursive: true
    });
    expect(fs.existsSync(`${temp.name}/test`)).to.be.false;
  });

  it('can remove folder non recursive', function() {
    output.mkdirSync('test');
    expect(fs.existsSync(`${temp.name}/test`)).to.be.true;
    expect(()=> output.rmdirSync('./')).to.throw(Error);
    output.rmdirSync('test');
    expect(fs.existsSync(`${temp.name}/test`)).to.be.false;
  });

  it('can symlinkOrCopySync', function () {
    let temp_in = tmp.dirSync().name;
    fs.writeFileSync(`${temp_in}/test.md`, 'test');
    output.symlinkOrCopySync(`${temp_in}/test.md`, `test.md`);
    expect(fs.realpathSync(`${temp.name}/test.md`)).to.be.contains(path.join(temp_in,'test.md'));
    expect(output.lstatSync('test.md').isSymbolicLink()).to.be.true;
  });

  it('can create file with outputFileSync', function () {
    output.outputFileSync(`asset/test.md`, `test`);
    expect(output.readFileSync('asset/test.md', 'utf-8')).to.be.equal('test');
  });

  it(`should throw if the dir strutcture doesn't exist and attempt to write`, function() {
    expect(() => output.writeFileSync('test/test.md', 'test')).to.throw(
      /.*no such file or directory.*/
    );
  });

  it(`should throw if the dir strutcture doesn't exist and attempt to read`, function() {
    expect(() => output.readFileSync('test/test.md')).to.throw(
      /ENOENT: no such file or directory,/
    );
  });
});