1 | import fs from 'fs-extra'
|
2 | import path from 'path'
|
3 | import test from 'ava'
|
4 | import sinon from 'sinon'
|
5 | import markdownMagic from '../index'
|
6 |
|
7 | const markdownPath = path.join(__dirname, 'fixtures', 'test.md')
|
8 | const outputDir = path.join(__dirname, 'fixtures', 'output')
|
9 | const DEBUG = false
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | test('if valid string path supplied', t => {
|
15 | markdownMagic(markdownPath)
|
16 | t.pass()
|
17 |
|
18 | })
|
19 |
|
20 | test('if valid glob pattern supplied', t => {
|
21 | const config = {
|
22 | outputDir: outputDir
|
23 | }
|
24 | markdownMagic(['test/fixtures/**/*md', '!test/fixtures/output/*.md'], config)
|
25 | t.pass()
|
26 |
|
27 |
|
28 | })
|
29 |
|
30 | test('if valid config supplied', t => {
|
31 | const config = {}
|
32 | markdownMagic(markdownPath, config)
|
33 | t.pass()
|
34 |
|
35 | })
|
36 |
|
37 | test('if callback function supplied, call it once', t => {
|
38 | const callback = sinon.spy()
|
39 | const config = {}
|
40 | markdownMagic(markdownPath, config, callback)
|
41 | t.true(callback.calledOnce)
|
42 |
|
43 | })
|
44 |
|
45 | test('if callback function supplied, as second arg, call it once', t => {
|
46 | const callback = sinon.spy()
|
47 | markdownMagic(markdownPath, callback)
|
48 | t.true(callback.calledOnce)
|
49 |
|
50 | })
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | test('if config.outputDir supplied, make new file', t => {
|
56 | const config = {
|
57 | outputDir: outputDir
|
58 | }
|
59 | markdownMagic(markdownPath, config, function() {
|
60 | const newfile = path.join(outputDir, 'test.md')
|
61 | const fileWasCreated = filePathExists(newfile)
|
62 | t.true(fileWasCreated)
|
63 |
|
64 |
|
65 | })
|
66 | })
|
67 |
|
68 | test('if config.matchWord supplied, use it for comment matching', t => {
|
69 | const filePath = path.join(__dirname, 'fixtures', 'custom-match-word-test.md')
|
70 | const config = {
|
71 | matchWord: 'YOLO',
|
72 | outputDir: outputDir
|
73 | }
|
74 | markdownMagic(filePath, config)
|
75 | const newfile = path.join(config.outputDir, 'custom-match-word-test.md')
|
76 | const newContent = fs.readFileSync(newfile, 'utf8')
|
77 | t.regex(newContent, /module\.exports\.run/, 'local code snippet inserted')
|
78 |
|
79 |
|
80 | fs.emptyDirSync(outputDir)
|
81 | })
|
82 |
|
83 | test('<!-- AUTO-GENERATED-CONTENT:START (TOC)-->', t => {
|
84 | const filePath = path.join(__dirname, 'fixtures', 'TOC-test.md')
|
85 | const config = {
|
86 | outputDir: outputDir
|
87 | }
|
88 | markdownMagic(filePath, config)
|
89 | const newfile = path.join(config.outputDir, 'TOC-test.md')
|
90 | const newContent = fs.readFileSync(newfile, 'utf8')
|
91 |
|
92 | const expectedTest1 = `
|
93 | <!-- AUTO-GENERATED-CONTENT:START (TOC) - Test #1: without option and the content with empty line -->
|
94 | - [Title A](#title-a)
|
95 | * [Subtitle z](#subtitle-z)
|
96 | * [Subtitle x](#subtitle-x)
|
97 | - [Title B](#title-b)
|
98 | - [Title C](#title-c)
|
99 | <!-- AUTO-GENERATED-CONTENT:END -->`
|
100 | const regexTest1 = new RegExp(`(?=${expectedTest1.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')})`, "i")
|
101 | t.regex(newContent, regexTest1, 'Test #1 : without option and the content with empty line')
|
102 |
|
103 | const expectedTest2 = `
|
104 | <!-- AUTO-GENERATED-CONTENT:START (TOC:collapse=true&collapseText=Click Me) - Test #2: with collapse options and the content with 'aaaaaaaaa' -->
|
105 | <details>
|
106 | <summary>Click Me</summary>
|
107 |
|
108 | - [Title A](#title-a)
|
109 | * [Subtitle z](#subtitle-z)
|
110 | * [Subtitle x](#subtitle-x)
|
111 | - [Title B](#title-b)
|
112 | - [Title C](#title-c)
|
113 |
|
114 | </details>
|
115 | <!-- AUTO-GENERATED-CONTENT:END -->`
|
116 | const regexTest2 = new RegExp(`(?=${expectedTest2.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')})`, "i")
|
117 | t.regex(newContent, regexTest2, "Test #2: with collapse options and the content with 'aaaaaaaaa'")
|
118 |
|
119 | const expectedTest3 = `
|
120 | <!-- AUTO-GENERATED-CONTENT:START (TOC:collapse=true&collapseText=Click Me=I have the power) - Test #3: with collapseText contains character '=' -->
|
121 | <details>
|
122 | <summary>Click Me=I have the power</summary>
|
123 |
|
124 | - [Title A](#title-a)
|
125 | * [Subtitle z](#subtitle-z)
|
126 | * [Subtitle x](#subtitle-x)
|
127 | - [Title B](#title-b)
|
128 | - [Title C](#title-c)
|
129 |
|
130 | </details>
|
131 | <!-- AUTO-GENERATED-CONTENT:END -->`
|
132 | const regexTest3 = new RegExp(`(?=${expectedTest3.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')})`, "i")
|
133 | t.regex(newContent, regexTest3, "Test #3: with collapseText contains character '='")
|
134 |
|
135 | const expectedTest4 = `
|
136 | <!-- AUTO-GENERATED-CONTENT:START (TOC) - Test #4: without option and the content is empty -->
|
137 | - [Title A](#title-a)
|
138 | * [Subtitle z](#subtitle-z)
|
139 | * [Subtitle x](#subtitle-x)
|
140 | - [Title B](#title-b)
|
141 | - [Title C](#title-c)
|
142 | <!-- AUTO-GENERATED-CONTENT:END -->`
|
143 | const regexTest4 = new RegExp(`(?=${expectedTest4.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')})`, "i")
|
144 | t.regex(newContent, regexTest4, 'Test #4 : without option and the content is empty')
|
145 |
|
146 | const expectedTest5 = `
|
147 | <!-- AUTO-GENERATED-CONTENT:START (TOC) - Test #5: without option and tags with same line -->
|
148 | - [Title A](#title-a)
|
149 | * [Subtitle z](#subtitle-z)
|
150 | * [Subtitle x](#subtitle-x)
|
151 | - [Title B](#title-b)
|
152 | - [Title C](#title-c)
|
153 | <!-- AUTO-GENERATED-CONTENT:END -->`
|
154 | const regexTest5 = new RegExp(`(?=${expectedTest5.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')})`, "i")
|
155 | t.regex(newContent, regexTest5, 'Test #5 : without option and tags with same line')
|
156 |
|
157 |
|
158 | fs.emptyDirSync(outputDir)
|
159 | })
|
160 |
|
161 |
|
162 |
|
163 |
|
164 | test('<!-- AUTO-GENERATED-CONTENT:START (CODE)-->', t => {
|
165 | const filePath = path.join(__dirname, 'fixtures', 'CODE-test.md')
|
166 | const config = { outputDir: outputDir }
|
167 | const newfile = path.join(config.outputDir, 'CODE-test.md')
|
168 |
|
169 | markdownMagic(filePath, config, function(err, data) {
|
170 |
|
171 | const newContent = fs.readFileSync(newfile, 'utf8')
|
172 |
|
173 | t.regex(newContent, /module\.exports\.run/, 'local code snippet inserted')
|
174 |
|
175 | t.regex(newContent, /require\('dox'\)/, 'remote code snippet inserted')
|
176 | })
|
177 |
|
178 | if (filePathExists(newfile)) {
|
179 |
|
180 | }
|
181 |
|
182 | })
|
183 |
|
184 | test('<!-- AUTO-GENERATED-CONTENT:START (REMOTE)-->', t => {
|
185 | const filePath = path.join(__dirname, 'fixtures', 'REMOTE-test.md')
|
186 |
|
187 | const config = { outputDir: outputDir }
|
188 | markdownMagic(filePath, config, function() {
|
189 | const newfile = path.join(config.outputDir, 'REMOTE-test.md')
|
190 | const newContent = fs.readFileSync(newfile, 'utf8')
|
191 |
|
192 | t.regex(newContent, /Markdown Magic/, 'word "Markdown Magic" not found in remote block')
|
193 |
|
194 | fs.emptyDirSync(outputDir)
|
195 | })
|
196 | })
|
197 |
|
198 |
|
199 |
|
200 |
|
201 | function filePathExists(fp) {
|
202 | try {
|
203 | fs.accessSync(fp)
|
204 | return true
|
205 | } catch (err) {
|
206 | return false
|
207 | }
|
208 | }
|
209 |
|
210 | function emptyDirectory(filePath, callBack) {
|
211 | fs.emptyDirSync(filePath)
|
212 | callBack && callBack(null)
|
213 | }
|