Skip to content

Commit

Permalink
Bug Fix: Images not getting read properly (#96)
Browse files Browse the repository at this point in the history
* Bug Fix: all files were reading in utf-8 which was preventing images from working properly

* CHange image to a free one

* Remove logging and update localhost homepage to put all fixed issues in there for future testing

* Add Github md alerts

* Add snapshot testing to all test folders

* Add header and paragraph details to local testing

* Add extra testing

* Add file count to snapshot testing

* testing testing table only

* remove hidden files making the tests fail in PR
  • Loading branch information
andysteadbbc authored Oct 25, 2024
1 parent 528b635 commit f6c96a5
Show file tree
Hide file tree
Showing 5 changed files with 314 additions and 12 deletions.
Binary file added default-md-files/folder/subFolder/test.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 45 additions & 1 deletion default-md-files/morty-docs.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,47 @@
# Morty docs
If you're seeing this, you haven't supplied an argument to the npm start command.

If you're seeing this, you haven't supplied an argument to the npm start command.
# Header 1
## Header 2
### Header 3
#### Header 4

## Paragraphs
### Bold
**Lorem Ipsum is simply dummy text of the printing and typesetting industry.**
### Italic
_Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book._
### Strike-Through
~~It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.~~
### Code line
`It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.`

## Coloured Links
lorem ipsum [this is a normal link](https://github.com/bbc) lorem ipsum<br>
[`this is a link within a code block`](https://github.com/bbc)
* link in a bullet point [`this is a link`](https://bbc-tpg.slack.com/archives/CH62XGS77) lorem ipsum

## Images
Do images still work?

![Image 1](./folder/subFolder/test.jpg)

## Github Alerts

> [!NOTE]
> Highlights information that users should take into account, even when skimming.
> More text note
> Even more text note
> [!TIP]
> Optional information to help a user be more successful.
> more text tip
> [!IMPORTANT]
> Crucial information necessary for users to succeed.
> [!WARNING]
> Critical content demanding immediate user attention due to potential risks.
> [!CAUTION]
> Negative potential consequences of an action.
6 changes: 4 additions & 2 deletions src/generate-transform-input.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ const generateTransformInput = (dir) => {
if (dirent.isSymbolicLink) {
if (fs.existsSync(fullPath)) { // fs.exists() is deprecated, but fs.existsSync() is not.
const stats = fs.statSync(fullPath)
console.log('Good symlink')
console.log('Good symlink', fullPath)
if (stats.isFile()) {
// get file details
list.push(makeInputObject(fullPath, dir)) // symlinks become copies
} else {
console.log('directory... continue')
// recursive call to get all files in the symlinked directory
const newlist = generateTransformInput(fullPath)
list = list.concat(newlist)
Expand All @@ -41,13 +42,14 @@ const generateTransformInput = (dir) => {
continue
}
}
console.log(list)
return list
}

const makeInputObject = (fullPath, rootPath) => {
return {
relativePath: fullPath.replace(`${rootPath}/`, ''),
raw: fs.readFileSync(fullPath, 'utf-8')
raw: fs.readFileSync(fullPath)
}
}

Expand Down
233 changes: 233 additions & 0 deletions test/unit/__snapshots__/generate-transform-input.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Snapshot Test Files in folder \`test1\`, should match snapshot with \`1\` files 1`] = `
Array [
Object {
"raw": Object {
"data": Array [
84,
101,
115,
116,
32,
49,
],
"type": "Buffer",
},
"relativePath": "test1.txt",
},
]
`;

exports[`Snapshot Test Files in folder \`test2\`, should match snapshot with \`2\` files 1`] = `
Array [
Object {
"raw": Object {
"data": Array [
68,
105,
114,
32,
49,
32,
84,
101,
115,
116,
32,
49,
],
"type": "Buffer",
},
"relativePath": "dir1/test1.txt",
},
Object {
"raw": Object {
"data": Array [
68,
105,
114,
32,
50,
32,
84,
101,
115,
116,
32,
49,
],
"type": "Buffer",
},
"relativePath": "dir2/test1.txt",
},
]
`;

exports[`Snapshot Test Files in folder \`test3\`, should match snapshot with \`3\` files 1`] = `
Array [
Object {
"raw": Object {
"data": Array [
68,
105,
114,
32,
49,
32,
84,
101,
115,
116,
32,
49,
],
"type": "Buffer",
},
"relativePath": "dir1/test1.txt",
},
Object {
"raw": Object {
"data": Array [
68,
105,
114,
32,
49,
32,
83,
117,
98,
32,
49,
32,
84,
101,
115,
116,
32,
49,
],
"type": "Buffer",
},
"relativePath": "dir1/sub1/test1.txt",
},
Object {
"raw": Object {
"data": Array [
68,
105,
114,
32,
49,
32,
83,
117,
98,
32,
50,
32,
84,
101,
115,
116,
32,
49,
],
"type": "Buffer",
},
"relativePath": "dir1/sub1/sub2/test1.txt",
},
]
`;

exports[`Snapshot Test Files in folder \`test4\`, should match snapshot with \`2\` files 1`] = `
Array [
Object {
"raw": Object {
"data": Array [
83,
121,
109,
108,
105,
110,
107,
32,
84,
101,
120,
116,
],
"type": "Buffer",
},
"relativePath": "symlink.txt",
},
Object {
"raw": Object {
"data": Array [
68,
105,
114,
32,
49,
32,
84,
101,
115,
116,
32,
49,
],
"type": "Buffer",
},
"relativePath": "dir1/test1.txt",
},
]
`;

exports[`Snapshot Test Files in folder \`test5\`, should match snapshot with \`1\` files 1`] = `
Array [
Object {
"raw": Object {
"data": Array [
83,
121,
109,
108,
105,
110,
107,
32,
84,
101,
120,
116,
],
"type": "Buffer",
},
"relativePath": "symlink.txt",
},
]
`;

exports[`Snapshot Test Files in folder \`test6\`, should match snapshot with \`0\` files 1`] = `Array []`;

exports[`Snapshot Test should match test1 snapshot 1`] = `
Array [
Object {
"raw": Object {
"data": Array [
84,
101,
115,
116,
32,
49,
],
"type": "Buffer",
},
"relativePath": "test1.txt",
},
]
`;
41 changes: 32 additions & 9 deletions test/unit/generate-transform-input.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@ describe('Test outputs for different returns from readdirSync', () => {

it('it returns all files in 1 directory', () => {
const actual = generateTransformInput('test/files/test1')
expect(actual).toEqual([{ raw: 'Test 1', relativePath: 'test1.txt' }])
expect(actual).toEqual([{ raw: expect.any(Object), relativePath: 'test1.txt' }])
})

it('it returns all files in multiple directories', () => {
const actual = generateTransformInput('test/files/test2')
expect(actual).toEqual([
{ raw: 'Dir 1 Test 1', relativePath: 'dir1/test1.txt' },
{ raw: 'Dir 2 Test 1', relativePath: 'dir2/test1.txt' }
{ raw: expect.any(Object), relativePath: 'dir1/test1.txt' },
{ raw: expect.any(Object), relativePath: 'dir2/test1.txt' }
])
})

it('it returns all files with sub directories', () => {
const actual = generateTransformInput('test/files/test3')
expect(actual).toEqual([
{ raw: 'Dir 1 Test 1', relativePath: 'dir1/test1.txt' },
{ raw: 'Dir 1 Sub 1 Test 1', relativePath: 'dir1/sub1/test1.txt' },
{ raw: 'Dir 1 Sub 2 Test 1', relativePath: 'dir1/sub1/sub2/test1.txt' }
{ raw: expect.any(Object), relativePath: 'dir1/test1.txt' },
{ raw: expect.any(Object), relativePath: 'dir1/sub1/test1.txt' },
{ raw: expect.any(Object), relativePath: 'dir1/sub1/sub2/test1.txt' }
])
})

Expand All @@ -49,8 +49,8 @@ describe('Symbolic links', () => {
it('has a good symlink to a directory, so include all files in the list', () => {
const actual = generateTransformInput('test/files/test4/dir1')
expect(actual).toEqual([
{ raw: 'Symlink Text', relativePath: 'symlink.txt' },
{ raw: 'Dir 1 Test 1', relativePath: 'test1.txt' }
{ raw: expect.any(Object), relativePath: 'symlink.txt' },
{ raw: expect.any(Object), relativePath: 'test1.txt' }
])
// cd test/files/test4/dir1
// ln -s ../good-symlink symlink - copy contents of symlink folder and put in dir1
Expand All @@ -59,7 +59,7 @@ describe('Symbolic links', () => {
it('has a good symlink to a file, so include it in the file list', () => {
const actual = generateTransformInput('test/files/test5')
expect(actual).toEqual([
{ raw: 'Symlink Text', relativePath: 'symlink.txt' }
{ raw: expect.any(Object), relativePath: 'symlink.txt' }
])
})

Expand All @@ -68,3 +68,26 @@ describe('Symbolic links', () => {
expect(actual).toEqual([])
})
})

describe('Snapshot Test', () => {
it('should match test1 snapshot', () => {
const actual = generateTransformInput('test/files/test1')
expect(actual).toMatchSnapshot()
})
})
describe('Snapshot Test', () => {
const cases = [
['test1', 1],
['test2', 2],
['test3', 3],
['test4', 2],
['test5', 1],
['test6', 0]
]

test.each(cases)('Files in folder `%s`, should match snapshot with `%s` files', async (folderName, totalFiles) => {
const actual = generateTransformInput(`test/files/${folderName}`)
expect(actual).toMatchSnapshot()
expect(actual.length).toEqual(totalFiles)
})
})

0 comments on commit f6c96a5

Please sign in to comment.