Passed
Push — master ( e51e0c...68cdf2 )
by Barry
01:05
created

test.js ➔ ... ➔ describe(ꞌreplaceLineEndingsꞌ)   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 10
rs 9.4285
c 1
b 0
f 0
1
/* global describe, it */
2
3
 const processFile = require('../dist/processFile.js').default
4
 const {processText} = require('../dist/processFile.js')
5
 const {findCode, findMdpInsert, findMdpCode, replaceLineEndings} = require('../dist/helpers.js')
6
 const {runCliCmd} = require('../dist/processFile.js')
7
 const assert = require('assert')
8
 const {exec} = require('child_process')
9
 const fs = require('fs-extra')
10
 const os = require('os')
11
12
 const mdpLinkTests = [
13
   {name: 'Simple 1', text: '[>]: # (mdpInsert abc)\r\n12345\r\n[<]: #\r\n', result: {start: 0, length: 37, internalStart: 24, internalLength: 5, commandString: 'mdpInsert abc'}},
14
   {name: 'Simple 2', text: 't\n[> a]: # (mdpInsert abc)\r\n12345\r\n[< b]: #\r\n', result: {start: 2, length: 41, internalStart: 28, internalLength: 5, commandString: 'mdpInsert abc'}},
15
   {name: 'Simple 3', text: '[>]: # (mdpInsert a)\r\n[<]: #\r\n', result: {start: 0, length: 28, internalStart: 22, internalLength: -2, commandString: 'mdpInsert a'}},
16
   {name: 'Simple 4', text: '[>]: # (mdpInsert abc)\r\n12345\r\n[<]: #', result: {start: 0, length: 37, internalStart: 24, internalLength: 5, commandString: 'mdpInsert abc'}},
17
   {name: 'Simple 5 (invalid)', text: '[#]: # (mdpInsert abc)\r\n12345\r\n[<]: #\r\n', result: {start: -1}},
18
   {name: 'code fenced', text: '[>]: # (mdpInsert abc)\r\n12345\r\n`[<]`: #\r\n', result: {start: -1}},
19
   {name: 'embedded', text: '[>]: # (mdpInsert abc)\r\n12\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[<]: #\r\n345\r\n[<]: #\r\n', result: {start: 0, length: 76, internalStart: 24, internalLength: 44, commandString: 'mdpInsert abc'}},
20
   {name: 'embedded, 5 deep', text: '[>]: # (mdpInsert abc)\r\n12\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[<]: #\r\n\r\n[<]: #\r\n\r\n[<]: #\r\n\r\n[<]: #\r\n345\r\n[<]: #\r\n', result: {start: 0, length: 193, internalStart: 24, internalLength: 161, commandString: 'mdpInsert abc'}},
21
   {name: 'embedded, too deep (6) - fails', text: '[>]: # (mdpInsert abc)\r\n12\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[<]: #\r\n\r\n[<]: #\r\n\r\n[<]: #\r\n\r\n[<]: #\r\n\r\n[<]: #\r\n345\r\n[<]: #\r\n', result: {start: -1}},
22
   {name: 'embedded, some fenced 1', text: '[>]: # (mdpInsert abc)\r\n12\r\n```\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n```\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[<]: #\r\n\r\n```\r\n[<]: #\r\n```\r\n[<]: #\r\n\r\n[<]: #\r\n\r\n[<]: #\r\n345\r\n[<]: #\r\n', result: {start: 0, length: 250, internalStart: 24, internalLength: 218, commandString: 'mdpInsert abc'}},
23
   {name: 'embedded, some fenced 2', text: '[>]: # (mdpInsert abc)\r\n12\r\n```\r\n[<]: #\r\n```\r\n[<]: #\r\n[<]: #\r\n', result: {start: 0, length: 52, internalStart: 24, internalLength: 20, commandString: 'mdpInsert abc'}},
24
   {name: 'embedded with following', text: '[>]: # (mdpInsert abc)\r\n12\r\n[>]: # (mdpInsert xyz)\r\nxyz\r\n[<]: #\r\n345\r\n[<]: #\r\n[>]: # (mdpInsert xyz)\r\n', result: {start: 0, length: 76, internalStart: 24, internalLength: 44, commandString: 'mdpInsert abc'}}
25
 ]
26
27
 const mdpCodeTests = [
28
   {name: 'Simple 1', text: '```json mdpInsert cat e.txt\r\ncode\r\n```', result: {start: 0, length: 38, internalStart: 29, internalLength: 4, commandString: 'json mdpInsert cat e.txt'}},
29
   {name: 'Fails mdpinsert spelt wrong', text: '```json mdpinsert cat e.txt\r\ncode\r\n```', result: {start: -1}},
30
   {name: 'Complex command line', text: 'text\r\n``` json   notes  mdpInsert cat e.txt --option1 sp --option2 \r\ncode\r\n```\r\n', result: {start: 6, length: 72, internalStart: 69, internalLength: 4, commandString: 'json   notes  mdpInsert cat e.txt --option1 sp --option2'}}
31
 ]
32
33
 const fencedCodeTests = [
34
   {name: 'Simple 1', text: 'plain\r\n```\r\ncode\r\n```\r\nplain', result: {start: 7, length: 14, internalStart: 12, internalLength: 4, commandString: ''}},
35
   {name: 'Simple 2', text: 'plain\n```\ncode\n```\nplain', result: {start: 6, length: 12, internalStart: 10, internalLength: 4, commandString: ''}},
36
   {name: 'Example 88', text: '```\n<\n >\n```', result: {start: 0, length: 12, internalStart: 4, internalLength: 4, commandString: ''}},
37
   {name: 'Example 89', text: '~~~\n<\n >\n~~~', result: {start: 0, length: 12, internalStart: 4, internalLength: 4, commandString: ''}},
38
   {name: 'Example 90', text: '``\nfoo\n``', result: {start: 0, length: 9, internalStart: 2, internalLength: 5, commandString: ''}},
39
   {name: 'Example 91', text: '```\naaa\n~~~\n```', result: {start: 0, length: 15, internalStart: 4, internalLength: 7, commandString: ''}},
40
   {name: 'Example 92', text: '~~~\naaa\n```\n~~~', result: {start: 0, length: 15, internalStart: 4, internalLength: 7, commandString: ''}},
41
   {name: 'Example 93', text: '````\naaa\n```\n``````', result: {start: 0, length: 19, internalStart: 5, internalLength: 7, commandString: ''}},
42
   {name: 'Example 94', text: '~~~~\naaa\n~~~\n~~~~', result: {start: 0, length: 17, internalStart: 5, internalLength: 7, commandString: ''}},
43
   {name: 'Example 95', text: '```', result: {start: 0, length: 3, internalStart: 3, internalLength: 0, commandString: ''}},
44
   {name: 'Example 96', text: '`````\n\n```\naaa', result: {start: 0, length: 14, internalStart: 6, internalLength: 8, commandString: ''}},
45
   {name: 'Example 97', text: '> ```\n> aaa\n\nbbb', result: {start: 0, length: 11, internalStart: 6, internalLength: 5, commandString: ''}},
46
   {name: 'Example 97a', text: '> ```\n> aaa\n> \n> bbb', result: {start: 0, length: 20, internalStart: 6, internalLength: 14, commandString: ''}},
47
   {name: 'Example 97b', text: '> ```\r\n> aaa\r\n\r\nbbb', result: {start: 0, length: 12, internalStart: 7, internalLength: 5, commandString: ''}},
48
   {name: 'Example 97c', text: '> ```\r\n> aaa\r\n> \r\n> bbb', result: {start: 0, length: 23, internalStart: 7, internalLength: 16, commandString: ''}},
49
   {name: 'Example 97d', text: '>```a\r\n>\r\nbbb', result: {start: 0, length: 8, internalStart: 7, internalLength: 1, commandString: 'a'}},
50
   {name: 'Example 98', text: '```\n\n  ```', result: {start: 0, length: 10, internalStart: 4, internalLength: 0, commandString: ''}},
51
   {name: 'Example 99', text: '```\n```', result: {start: 0, length: 7, internalStart: 4, internalLength: -1, commandString: ''}},
52
   {name: 'Example 100', text: ' ```\n aaa\naaa\n```', result: {start: 0, length: 17, internalStart: 5, internalLength: 8, commandString: ''}},
53
   {name: 'Example 101', text: '  ```\naaa\n  aaa\naaa\n  ```', result: {start: 0, length: 25, internalStart: 6, internalLength: 13, commandString: ''}},
54
   {name: 'Example 101b', text: '  ```\naaa\n  aaa\naaa\n  ```\r\n', result: {start: 0, length: 25, internalStart: 6, internalLength: 13, commandString: ''}},
55
   {name: 'Example 101c', text: '  ```\naaa\n  aaa\naaa\n  ```\n', result: {start: 0, length: 25, internalStart: 6, internalLength: 13, commandString: ''}},
56
   {name: 'Example 101d', text: '\r\n  ```\naaa\n  aaa\naaa\n  ```\r\n', result: {start: 2, length: 25, internalStart: 8, internalLength: 13, commandString: ''}},
57
   {name: 'Example 101e', text: '\n  ```\naaa\n  aaa\naaa\n  ```\r\n', result: {start: 1, length: 25, internalStart: 7, internalLength: 13, commandString: ''}},
58
   {name: 'Example 102', text: '   ```\n   aaa\n    aaa\n   ```', result: {start: 0, length: 28, internalStart: 7, internalLength: 14, commandString: ''}},
59
   {name: 'Example 103', text: '    ```\n    aaa\n    ```', result: {start: 0, length: 23, internalStart: 0, internalLength: 23, commandString: ''}},
60
   {name: 'Example 104', text: '```\naaa\n  ```', result: {start: 0, length: 13, internalStart: 4, internalLength: 3, commandString: ''}},
61
   {name: 'Example 105', text: '   ```\naaa\n  ```', result: {start: 0, length: 16, internalStart: 7, internalLength: 3, commandString: ''}},
62
   {name: 'Example 106', text: '```\naaa\n    ```', result: {start: 0, length: 15, internalStart: 4, internalLength: 11, commandString: ''}},
63
   {name: 'Example 107', text: '``` ```\naaa', result: {start: 0, length: 7, internalStart: 3, internalLength: 1, commandString: ''}},
64
   {name: 'Example 108', text: '~~~~~~\naaa\n~~~ ~~', result: {start: 0, length: 17, internalStart: 7, internalLength: 10, commandString: ''}},
65
   {name: 'Example 109', text: 'foo\n```\nbar\n```\nbaz', result: {start: 4, length: 11, internalStart: 8, internalLength: 3, commandString: ''}},
66
   {name: 'Example 110', text: 'foo\n---\n~~~\nbar\n~~~\n# baz', result: {start: 8, length: 11, internalStart: 12, internalLength: 3, commandString: ''}},
67
   {name: 'Example 111', text: '```ruby\ndef foo(x)\n  return 3\nend\n```', result: {start: 0, length: 37, internalStart: 8, internalLength: 25, commandString: 'ruby'}},
68
   {name: 'Example 112', text: '~~~~    ruby startline=3 $%@#$\r\ndef foo(x)\r\n  return 3\r\nend\r\n~~~~~~~', result: {start: 0, length: 68, internalStart: 32, internalLength: 27, commandString: 'ruby startline=3 $%@#$'}},
69
   {name: 'Example 113', text: '````;\r\n````', result: {start: 0, length: 11, internalStart: 7, internalLength: -2, commandString: ';'}},
70
   {name: 'Example 114', text: '``` aa ```\r\nfoo', result: {start: 0, length: 10, internalStart: 3, internalLength: 4, commandString: ''}},
71
   {name: 'Example 115', text: '```\r\n``` aaa\r\n```', result: {start: 0, length: 17, internalStart: 5, internalLength: 7, commandString: ''}},
72
   {name: 'Example 324', text: '`foo`', result: {start: 0, length: 5, internalStart: 1, internalLength: 3, commandString: ''}},
73
   {name: 'Example 325', text: '`` foo ` bar  ``', result: {start: 0, length: 16, internalStart: 2, internalLength: 12, commandString: ''}},
74
   {name: 'Example 326', text: '` `` `', result: {start: 0, length: 6, internalStart: 1, internalLength: 4, commandString: ''}},
75
   {name: 'Example 327', text: '``\r\nfoo\r\n``', result: {start: 0, length: 11, internalStart: 2, internalLength: 7, commandString: ''}},
76
   {name: 'Example 328', text: '`foo   bar\r\n  baz`', result: {start: 0, length: 18, internalStart: 1, internalLength: 16, commandString: ''}},
77
   {name: 'Example 329', text: '`a b`', result: {start: 0, length: 5, internalStart: 1, internalLength: 3, commandString: ''}},
78
   {name: 'Example 330', text: '`foo `` bar`', result: {start: 0, length: 12, internalStart: 1, internalLength: 10, commandString: ''}},
79
   {name: 'Example 331', text: '`foo\\`bar`', result: {start: 0, length: 6, internalStart: 1, internalLength: 4, commandString: ''}},
80
   {name: 'Example 332', text: '*foo`*`', result: {start: 4, length: 3, internalStart: 5, internalLength: 1, commandString: ''}},
81
   {name: 'Example 333', text: '[not a `link](/foo`)', result: {start: 7, length: 12, internalStart: 8, internalLength: 10, commandString: ''}},
82
   {name: 'Example 334', text: '`<a href="`">`', result: {start: 0, length: 11, internalStart: 1, internalLength: 9, commandString: ''}},
83
   {name: 'Example 336', text: '`<http://foo.bar.`baz>`', result: {start: 0, length: 18, internalStart: 1, internalLength: 16, commandString: ''}},
84
   {name: 'Example 338', text: '```foo``', result: {start: -1}},
85
   {name: 'Example 339', text: '`foo', result: {start: -1}},
86
   {name: 'Example 340', text: '`foo``bar``', result: {start: 4, length: 7, internalStart: 6, internalLength: 3, commandString: ''}},
87
   {name: 'block and code 1', text: '>```\r\n>code\r\ntext', result: {start: 0, length: 11, internalStart: 6, internalLength: 5, commandString: ''}},
88
   {name: 'block and code 1b', text: '>```\r\n>code\ntext', result: {start: 0, length: 11, internalStart: 6, internalLength: 5, commandString: ''}},
89
   {name: 'block and code 2', text: '>```\r\n>code\r\ntext```\r\n', result: {start: 0, length: 11, internalStart: 6, internalLength: 5, commandString: ''}},
90
   {name: 'block and code 3', text: '>```\r\n>code\r\n>```\r\ntext', result: {start: 0, length: 17, internalStart: 6, internalLength: 5, commandString: ''}}
91
 ]
92
93
 const lineEndingTests = [
94
   {name: '1', text: '\nabc\r\nabc\n\n', resultCRLF: '\r\nabc\r\nabc\r\n\r\n', resultLF: '\nabc\nabc\n\n'},
95
   {name: '2', text: ' \n\nabc\n\rabc\n ', resultCRLF: ' \r\n\r\nabc\r\n\rabc\r\n ', resultLF: ' \n\nabc\n\rabc\n '},
96
   {name: '3', text: '\r\nabc\nabc\r\n', resultCRLF: '\r\nabc\r\nabc\r\n', resultLF: '\nabc\nabc\n'},
97
   {name: '4', text: ' \r\n\r\nabc\r\n\rabc\r\n ', resultCRLF: ' \r\n\r\nabc\r\n\rabc\r\n ', resultLF: ' \n\nabc\n\rabc\n '}
98
 ]
99
100
 const processTextTests = [
101
   {
102
     name: 'Simple 1',
103
     text: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\n12345\r\n[<]: #\r\n',
104
     clear: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\n[<]: #\r\n',
105
     full_win: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\n',
106
     full_linux: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\n'
107
   }, {
108
     name: 'mdpInsert command not present',
109
     text: '[>]: # (mdpinsert cat test/docs/abc.txt)\r\n12345\r\n[<]: #\r\n',
110
     clear: '[>]: # (mdpinsert cat test/docs/abc.txt)\r\n12345\r\n[<]: #\r\n',
111
     full_win: '[>]: # (mdpinsert cat test/docs/abc.txt)\r\n12345\r\n[<]: #\r\n',
112
     full_linux: '[>]: # (mdpinsert cat test/docs/abc.txt)\r\n12345\r\n[<]: #\r\n'
113
   }, {
114
     name: 'Command Line invalid',
115
     text: '[>]: # (mdpInsert catt test/docs/abc.txt)\r\n12345\r\n[<]: #\r\n',
116
     clear: '[>]: # (mdpInsert catt test/docs/abc.txt)\r\n[<]: #\r\n',
117
     full_win: '[>]: # (mdpInsert catt test/docs/abc.txt)\r\nERROR: Command failed: catt test/docs/abc.txt\r\n\'catt\' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n\r\n[<]: #\r\n',
118
     full_linux: '[>]: # (mdpInsert catt test/docs/abc.txt)\r\nERROR: Command failed: catt test/docs/abc.txt\r\n/bin/sh: 1: catt: not found\r\n\r\n[<]: #\r\n'
119
   }, {
120
     name: 'Surrounded',
121
     text: '# Simple Test\r\nSome initial text.\r\n[>]: # (mdpInsert cat test/docs/abc.txt)\r\nold text\r\n[<]: #\r\nOther text',
122
     clear: '# Simple Test\r\nSome initial text.\r\n[>]: # (mdpInsert cat test/docs/abc.txt)\r\n[<]: #\r\nOther text',
123
     full_win: '# Simple Test\r\nSome initial text.\r\n[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\nOther text',
124
     full_linux: '# Simple Test\r\nSome initial text.\r\n[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\nOther text'
125
   }, {
126
     name: 'Empty',
127
     text: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\n\r\n[<]: #\r\n',
128
     clear: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\n[<]: #\r\n',
129
     full_win: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\n',
130
     full_linux: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\n'
131
   }, {
132
     name: 'Blank Line',
133
     text: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\n[<]: #\r\n',
134
     clear: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\n[<]: #\r\n',
135
     full_linux: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\n',
136
     full_win: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\n'
137
   }, {
138
     name: 'LF only',
139
     text: '[>]: # (mdpInsert cat test/docs/abc.txt)\n[<]: #\n',
140
     clear: '[>]: # (mdpInsert cat test/docs/abc.txt)\n[<]: #\n',
141
     full_win: '[>]: # (mdpInsert cat test/docs/abc.txt)\nabc\n[<]: #\n',
142
     full_linux: '[>]: # (mdpInsert cat test/docs/abc.txt)\nabc\n[<]: #\n'
143
   }, {
144
     name: 'add code fence 1',
145
     text: '[>]: # (mdpInsert ```json cat test/docs/abc.txt)\n[<]: #\n',
146
     clear: '[>]: # (mdpInsert ```json cat test/docs/abc.txt)\n[<]: #\n',
147
     full_win: '[>]: # (mdpInsert ```json cat test/docs/abc.txt)\n```json\nabc\n```\n[<]: #\n',
148
     full_linux: '[>]: # (mdpInsert ```json cat test/docs/abc.txt)\n```json\nabc\n```\n[<]: #\n'
149
   }, {
150
     name: 'add code fence 2',
151
     text: '[>]: # (mdpInsert ```json cat test/docs/abc.txt)\r\n[<]: #\r\n',
152
     clear: '[>]: # (mdpInsert ```json cat test/docs/abc.txt)\r\n[<]: #\r\n',
153
     full_win: '[>]: # (mdpInsert ```json cat test/docs/abc.txt)\r\n```json\r\nabc\r\n```\r\n[<]: #\r\n',
154
     full_linux: '[>]: # (mdpInsert ```json cat test/docs/abc.txt)\r\n```json\r\nabc\r\n```\r\n[<]: #\r\n'
155
   }, {
156
     name: 'add code fence 3',
157
     text: '[>]: # (mdpInsert ~~~~json cat test/docs/abc.txt)\r\n[<]: #\r\n',
158
     clear: '[>]: # (mdpInsert ~~~~json cat test/docs/abc.txt)\r\n[<]: #\r\n',
159
     full_win: '[>]: # (mdpInsert ~~~~json cat test/docs/abc.txt)\r\n~~~~json\r\nabc\r\n~~~~\r\n[<]: #\r\n',
160
     full_linux: '[>]: # (mdpInsert ~~~~json cat test/docs/abc.txt)\r\n~~~~json\r\nabc\r\n~~~~\r\n[<]: #\r\n'
161
   }, {
162
     name: 'Two replacements',
163
     text: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\n12345\r\n[<]: #\r\n[>]: # (mdpInsert cat test/docs/abc.txt)\r\n12345\r\n[<]: #\r\n',
164
     clear: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\n[<]: #\r\n[>]: # (mdpInsert cat test/docs/abc.txt)\r\n[<]: #\r\n',
165
     full_win: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\n[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\n',
166
     full_linux: '[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\n[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\n'
167
   }, {
168
     name: 'Code Fence 1',
169
     text: '```json mdpInsert cat test/docs/abc.txt\r\nxyz\r\ncode\r\n```',
170
     clear: '```json mdpInsert cat test/docs/abc.txt\r\n```',
171
     full_win: '```json mdpInsert cat test/docs/abc.txt\r\nabc\r\n```',
172
     full_linux: '```json mdpInsert cat test/docs/abc.txt\r\nabc\r\n```'
173
   }, {
174
     name: 'Code Fence 2',
175
     text: 'ss\r\n```json mdpInsert cat test/docs/abc.txt\r\n```',
176
     clear: 'ss\r\n```json mdpInsert cat test/docs/abc.txt\r\n```',
177
     full_win: 'ss\r\n```json mdpInsert cat test/docs/abc.txt\r\nabc\r\n```',
178
     full_linux: 'ss\r\n```json mdpInsert cat test/docs/abc.txt\r\nabc\r\n```'
179
   }, {
180
     name: 'Code Fence and link',
181
     text: '```json mdpInsert cat test/docs/abc.txt\r\nxyz\r\ncode\r\n```\r\n[>]: # (mdpInsert cat test/docs/abc.txt)\r\n12345\r\n[<]: #\r\n',
182
     clear: '```json mdpInsert cat test/docs/abc.txt\r\n```\r\n[>]: # (mdpInsert cat test/docs/abc.txt)\r\n[<]: #\r\n',
183
     full_win: '```json mdpInsert cat test/docs/abc.txt\r\nabc\r\n```\r\n[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\n',
184
     full_linux: '```json mdpInsert cat test/docs/abc.txt\r\nabc\r\n```\r\n[>]: # (mdpInsert cat test/docs/abc.txt)\r\nabc\r\n[<]: #\r\n'
185
   }, {
186
     name: 'Missing File',
187
     text: '[>]: # (mdpInsert cat file/not/present.txt)\r\n12345\r\n[<]: #\r\n',
188
     clear: '[>]: # (mdpInsert cat file/not/present.txt)\r\n[<]: #\r\n',
189
     full_win: '[>]: # (mdpInsert cat file/not/present.txt)\r\nERROR: Command failed: type file\\not\\present.txt\r\nThe system cannot find the path specified.\r\n\r\n[<]: #\r\n',
190
     full_linux: '[>]: # (mdpInsert cat file/not/present.txt)\r\nERROR: Command failed: cat file/not/present.txt\r\ncat: file/not/present.txt: No such file or directory\r\n\r\n[<]: #\r\n'
191
   }
192
 ]
193
194
// describe('mdprepare', function () {
195
//   it('fires and returns "processed x files message"', function (done) {
196
//     this.timeout(4000)
197
//     let buf = execSync('mdprepare --ignore **/*.js')
198
//     assert.equal(buf.toString().substr(0, 10), 'processed ')
199
//     done()
200
//   })
201
// })
202
203
 describe('unit tests', function () {
204
   describe('helpers.js', function () {
205
     describe('replaceLineEndings', function () {
206
       for (let i = 0; i < lineEndingTests.length; i++) {
207
         it(lineEndingTests[i].name + ' CRLF', function () {
208
           assert.equal(replaceLineEndings(lineEndingTests[i].text, true), lineEndingTests[i].resultCRLF)
209
         })
210
         it(lineEndingTests[i].name + ' LF', function () {
211
           assert.equal(replaceLineEndings(lineEndingTests[i].text, false), lineEndingTests[i].resultLF)
212
         })
213
       }
214
     })
215
     describe('findCode', function () {
216
       for (let i = 0; i < fencedCodeTests.length; i++) {
217
         it(fencedCodeTests[i].name, function () {
218
           let r = findCode(fencedCodeTests[i].text)
219
           delete r.info
220
           assert.deepEqual(r, fencedCodeTests[i].result)
221
         })
222
       }
223
     })
224
     describe('findMdpInsertBlocks', function () {
225
       for (let i = 0; i < mdpLinkTests.length; i++) {
226
         it(mdpLinkTests[i].name, function () {
227
           let r = findMdpInsert(mdpLinkTests[i].text)
228
           delete r.info
229
           assert.deepEqual(r, mdpLinkTests[i].result)
230
         })
231
       }
232
     })
233
     describe('findMdpCode', function () {
234
       for (let i = 0; i < mdpCodeTests.length; i++) {
235
         it(mdpCodeTests[i].name, function () {
236
           let r = findMdpCode(mdpCodeTests[i].text)
237
           delete r.info
238
           assert.deepEqual(r, mdpCodeTests[i].result)
239
         })
240
       }
241
     })
242
     describe('runCliCmd', function () {
243
       it('Simple', function () {
244
         assert.equal(runCliCmd('cat abc.txt', 'test/docs').toString(), 'abc')
245
       })
246
       it('invalid', function () {
247
         if (os.platform() === 'win32') {
248
           assert.equal(runCliCmd('catt abc.txt', 'test/docs').toString(), 'ERROR: Command failed: catt abc.txt\n\'catt\' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n')
249
         } else {
250
           assert.equal(runCliCmd('catt abc.txt', 'test/docs').toString(), 'ERROR: Command failed: catt abc.txt\n/bin/sh: 1: catt: not found\n')
251
         }
252
       })
253
     })
254
     describe('processText', function () {
255
       for (let i = 0; i < processTextTests.length; i++) {
256
         it(processTextTests[i].name + ' --clear', function () {
257
           let r = processText(processTextTests[i].text, true, process.cwd())
258
           assert.equal(r, processTextTests[i].clear)
259
         })
260
         it(processTextTests[i].name + ' full', function () {
261
           let r = processText(processTextTests[i].text, false, process.cwd())
262
           if (os.platform() === 'win32') {
263
             assert.equal(r, processTextTests[i].full_win)
264
           } else {
265
             assert.equal(r, processTextTests[i].full_linux)
266
           }
267
         })
268
       }
269
     })
270
     describe('processFile.js', function () {
271
       it('reference to processFile.js OK', function () {
272
         assert(processFile)
273
       })
274
       it('process file against missing file should error', function () {
275
         assert.throws(function () { processFile('not a file') }, /file not found/)
276
       })
277
     })
278
   })
279
 })
280
281
 describe('mdprepare', function () {
282
   it('fires and returns "processed x files message (1)"', function (done) {
283
     this.timeout(8000)
284
     exec('mdprepare --ignore=**/*.md', function (error, stdout) {
285
       assert.ifError(error)
286
       assert.notEqual(stdout.toString().indexOf('processed 0 files'), -1)
287
       done()
288
     })
289
   })
290
291
   it('fires and returns "processed x files message (2)"', function (done) {
292
     this.timeout(8000)
293
     exec('mdprepare fred', function (error, stdout) {
294
       assert.ifError(error)
295
       assert.notEqual(stdout.toString().indexOf('processed 0 files'), -1)
296
       done()
297
     })
298
   })
299
300
   it('fires and returns error if directory not found', function (done) {
301
     this.timeout(8000)
302
     exec('mdprepare //', function (error, stdout, stderr) {
303
       assert.ifError(error)
304
       assert.notEqual(stderr.toString(), '')
305
       done()
306
     })
307
   })
308
 })
309
310
 describe('mdprepare test files', function () {
311
   fs.emptyDirSync('./test/preparedClear')
312
   fs.copySync('./test/docs', './test/preparedClear')
313
   fs.emptyDirSync('./test/preparedFull')
314
   fs.copySync('./test/docs', './test/preparedFull')
315
   var files = fs.readdirSync('./test/docs')
316
   for (let i = 0; i < files.length; i++) {
317
     if (files[i].substr(-3) === '.md') {
318
       it('/test/docs/' + files[i] + ' is prepared correctly', function (done) {
319
         this.timeout(8000)
320
         exec('mdprepare ./test/preparedFull/' + files[i], function (error) {
321
           assert.ifError(error)
322
           assert.equal(fs.readFileSync('./test/preparedFull/' + files[i], 'utf8'), fs.readFileSync('./test/preparedFull/' + files[i] + '.full', 'utf8'))
323
           done()
324
         })
325
       })
326
       it('/test/docs/' + files[i] + ' is cleared correctly', function (done) {
327
         this.timeout(8000)
328
         exec('mdprepare ./test/preparedClear/' + files[i] + ' --clear', function (error) {
329
           assert.ifError(error)
330
           assert.equal(fs.readFileSync('./test/preparedClear/' + files[i], 'utf8'), fs.readFileSync('./test/preparedClear/' + files[i] + '.clear', 'utf8'))
331
           done()
332
         })
333
       })
334
     }
335
   }
336
   it('test working with read only file - should error', function (done) {
337
     this.timeout(8000)
338
     fs.writeFileSync('./test/preparedClear/readonly.md', '[>]: # (mdpInsert cat test/docs/abc.txt)\r\n12345\r\n[<]: #\r\n')
339
     fs.chmodSync('./test/preparedClear/readonly.md', 0o444) // make file read only
340
     exec('mdprepare ./test/preparedClear/readonly.md', function (error, stdout, stderr) {
341
       assert.ifError(error)
342
       assert.notEqual(stderr.toString(), '')
343
       done()
344
     })
345
   })
346
 })
347