11import { expect } from 'chai' ;
22import * as path from 'path' ;
33import * as fs from 'fs-extra' ;
4- import { generateMarkdown } from '../../../../src/remote-queries/remote-queries-markdown-generation' ;
5-
6- const expectedFileNames = [ 'summary' , 'github-codeql' , 'meteor-meteor' ] ;
4+ import { generateMarkdown , MarkdownFile } from '../../../../src/remote-queries/remote-queries-markdown-generation' ;
75
86describe ( 'markdown generation' , async function ( ) {
97 describe ( 'for path-problem query' , async function ( ) {
@@ -15,20 +13,10 @@ describe('markdown generation', async function() {
1513 const analysesResults = JSON . parse (
1614 await fs . readFile ( path . join ( __dirname , 'data/interpreted-results/path-problem/analyses-results.json' ) , 'utf8' )
1715 ) ;
18- const markdownFiles = generateMarkdown ( pathProblemQuery , analysesResults , 'gist' ) ;
19-
20- // Check that query has results for two repositories, plus a summary file
21- expect ( markdownFiles . length ) . to . equal ( 3 ) ;
2216
23- for ( let i = 0 ; i < markdownFiles . length ; i ++ ) {
24- const markdownFile = markdownFiles [ i ] ;
25- const expectedContent = await readTestOutputFile ( `data/interpreted-results/path-problem/expected/${ expectedFileNames [ i ] } .md` ) ;
17+ const actualFiles = generateMarkdown ( pathProblemQuery , analysesResults , 'gist' ) ;
2618
27- // Check that the markdown file has the expected name
28- expect ( markdownFile . fileName ) . to . equal ( expectedFileNames [ i ] ) ;
29- // Check that the markdown file has the expected content
30- expect ( markdownFile . content . join ( '\n' ) ) . to . equal ( expectedContent ) ;
31- }
19+ await checkGeneratedMarkdown ( actualFiles , 'data/interpreted-results/path-problem/expected' ) ;
3220 } ) ;
3321 } ) ;
3422
@@ -41,20 +29,9 @@ describe('markdown generation', async function() {
4129 const analysesResults = JSON . parse (
4230 await fs . readFile ( path . join ( __dirname , 'data/interpreted-results/problem/analyses-results.json' ) , 'utf8' )
4331 ) ;
44- const markdownFiles = generateMarkdown ( problemQuery , analysesResults , 'gist' ) ;
45-
46- // Check that query has results for two repositories, plus a summary file
47- expect ( markdownFiles . length ) . to . equal ( 3 ) ;
32+ const actualFiles = generateMarkdown ( problemQuery , analysesResults , 'gist' ) ;
4833
49- for ( let i = 0 ; i < markdownFiles . length ; i ++ ) {
50- const markdownFile = markdownFiles [ i ] ;
51- const expectedContent = await readTestOutputFile ( `data/interpreted-results/problem/expected/${ expectedFileNames [ i ] } .md` ) ;
52-
53- // Check that the markdown file has the expected name
54- expect ( markdownFile . fileName ) . to . equal ( expectedFileNames [ i ] ) ;
55- // Check that the markdown file has the expected content
56- expect ( markdownFile . content . join ( '\n' ) ) . to . equal ( expectedContent ) ;
57- }
34+ await checkGeneratedMarkdown ( actualFiles , 'data/interpreted-results/problem/expected' ) ;
5835 } ) ;
5936 } ) ;
6037
@@ -67,20 +44,9 @@ describe('markdown generation', async function() {
6744 await fs . readFile ( path . join ( __dirname , 'data/raw-results/analyses-results.json' ) , 'utf8' )
6845 ) ;
6946
70- const markdownFiles = generateMarkdown ( query , analysesResults , 'gist' ) ;
71-
72- // Check that query has results for two repositories, plus a summary file
73- expect ( markdownFiles . length ) . to . equal ( 3 ) ;
47+ const actualFiles = generateMarkdown ( query , analysesResults , 'gist' ) ;
7448
75- for ( let i = 0 ; i < markdownFiles . length ; i ++ ) {
76- const markdownFile = markdownFiles [ i ] ;
77- const expectedContent = await readTestOutputFile ( `data/raw-results/expected/${ expectedFileNames [ i ] } .md` ) ;
78-
79- // Check that the markdown file has the expected name
80- expect ( markdownFile . fileName ) . to . equal ( expectedFileNames [ i ] ) ;
81- // Check that the markdown file has the expected content
82- expect ( markdownFile . content . join ( '\n' ) ) . to . equal ( expectedContent ) ;
83- }
49+ await checkGeneratedMarkdown ( actualFiles , 'data/raw-results/expected' ) ;
8450 } ) ;
8551 } ) ;
8652} ) ;
@@ -93,3 +59,21 @@ async function readTestOutputFile(relativePath: string): Promise<string> {
9359 const file = await fs . readFile ( path . join ( __dirname , relativePath ) , 'utf8' ) ;
9460 return file . replace ( / \r ? \n / g, '\n' ) ;
9561}
62+
63+ /**
64+ * Compares the generated (actual) markdown files to the expected markdown files and
65+ * checks whether the names and contents are the same.
66+ */
67+ async function checkGeneratedMarkdown ( actualFiles : MarkdownFile [ ] , testDataBasePath : string ) {
68+ const expectedDir = path . join ( __dirname , testDataBasePath ) ;
69+ const expectedFiles = await fs . readdir ( expectedDir ) ;
70+
71+ expect ( actualFiles . length ) . to . equal ( expectedFiles . length ) ;
72+
73+ for ( const expectedFile of expectedFiles ) {
74+ const actualFile = actualFiles . find ( f => `${ f . fileName } .md` === expectedFile ) ;
75+ expect ( actualFile ) . to . not . be . undefined ;
76+ const expectedContent = await readTestOutputFile ( path . join ( testDataBasePath , expectedFile ) ) ;
77+ expect ( actualFile ! . content . join ( '\n' ) ) . to . equal ( expectedContent ) ;
78+ }
79+ }
0 commit comments