Skip to content

Commit

Permalink
added lots of formats in tests - compare and assert on info on each o…
Browse files Browse the repository at this point in the history
…f them
  • Loading branch information
cancerberoSgx committed Nov 12, 2018
1 parent 35785c8 commit bf038b4
Show file tree
Hide file tree
Showing 33 changed files with 4,347 additions and 30 deletions.
File renamed without changes.
Binary file added spec/assets/formats/to_rotate.dcm
Binary file not shown.
Binary file added spec/assets/formats/to_rotate.dds
Binary file not shown.
Binary file added spec/assets/formats/to_rotate.djvu
Binary file not shown.
Binary file added spec/assets/formats/to_rotate.fits
Binary file not shown.
File renamed without changes
Binary file added spec/assets/formats/to_rotate.hdr
Binary file not shown.
Binary file added spec/assets/formats/to_rotate.ico
Binary file not shown.
Binary file added spec/assets/formats/to_rotate.inline
Binary file not shown.
File renamed without changes
Binary file added spec/assets/formats/to_rotate.miff
Binary file not shown.
Binary file added spec/assets/formats/to_rotate.mng
Binary file not shown.
Binary file added spec/assets/formats/to_rotate.otb
Binary file not shown.
Binary file added spec/assets/formats/to_rotate.pcx
Binary file not shown.
Binary file added spec/assets/formats/to_rotate.pfm
Binary file not shown.
Binary file added spec/assets/formats/to_rotate.pgm
Binary file not shown.
Binary file added spec/assets/formats/to_rotate.pix
Binary file not shown.
File renamed without changes
Binary file added spec/assets/formats/to_rotate.pnm
Binary file not shown.
Binary file added spec/assets/formats/to_rotate.ppm
Binary file not shown.
File renamed without changes.
Binary file added spec/assets/formats/to_rotate.rgb
Binary file not shown.
Binary file added spec/assets/formats/to_rotate.tga
Binary file not shown.
File renamed without changes.
4,097 changes: 4,097 additions & 0 deletions spec/assets/formats/to_rotate.txt

Large diffs are not rendered by default.

File renamed without changes.
87 changes: 87 additions & 0 deletions spec/assets/formats/to_rotate.xpm
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/* XPM */
static char * to_rotate_xpm[] = {
"64 64 20 1",
" g #FFFFFF",
". g #FEFEFE",
"+ g #000000",
"@ g #010101",
"# g #FCFCFC",
"$ g #FDFDFD",
"% g #040404",
"& g #030303",
"* g #F8F8F8",
"= g #FAFAFA",
"- g #FBFBFB",
"; g #F9F9F9",
"> g #F7F7F7",
", g #020202",
"' g #050505",
") g #F4F4F4",
"! g #070707",
"~ g #060606",
"{ g #090909",
"] g #0C0C0C",
" . .+++++++++++++++++++ .. ",
" . .++++++++++++++++++@ . #. ",
" . .+++++++++++++++++++$ .. . ",
" . .++++++++++++++++++%$ $ $ ",
" . .+++++++++++++++++++@ # $ ",
" . .++++++++++++++++++&@$. ",
" . .++++++++++++++++++++ .$. ",
" . .++++++++++++++++++@+. $$ ",
" @++++++++++++++++++++. . ",
" @++++++++++++++++++++. . ",
" @++++++++++++++++++++. . ",
" @++++++++++++++++++++. . ",
" @++++++++++++++++++++. . ",
" @++++++++++++++++++++. . ",
" @++++++++++++++++++++. . ",
" @++++++++++++++++++++. . ",
" @+++++++++++++++++++. * = ",
" @+++++++++++++++++++ $. # - ",
" @++++++++++++++++++ *. = ; ",
" @++++++++++++++++++. #. ",
" @++++++++++++++++++ #== ",
" @++++++++++++++++++ ## ",
" @++++++++++++++++++# # = ",
" @++++++++++++++++++ .# # . ",
" @++++++++++++++++++ ",
" @++++++++++++++++++ ",
" @++++++++++++++++++ ",
" @++++++++++++++++++ ",
" @++++++++++++++++++ ",
" @++++++++++++++++++ ",
" @++++++++++++++++++ ",
" @++++++++++++++++++ ",
" @++++++++++++++++++ - $ ",
" @++++++++++++++++++ # ; ",
" @++++++++++++++++++. ; - ",
" @++++++++++++++++++@> - $ # ",
" @++++++++++++++++++&%# . ",
" @++++++++++++++++++++, . ",
" @++++++++++++++++++%&++&$ . ",
" @++++++++++++++++++++,&+, # ",
" .# +@++@&++&+@++++++++++++++++. $ . $ $ *.",
" $- . -++&+++&++'+++++++++++++++++ = # # >$ ",
" $ .= '+++&&++++++++++++++++++++,+ # # ; $* -",
" ) = $++++++&''++++++++++++++++++,. . -. ",
" - .$$ &@++&@+++++++++++++++++++&%++ #= .$ -.#- ",
" * $$+!@+'+~+++++++++++++++++++,+~+. . # .",
" $ .# # +,@+&+%++++++++++++++++@%~+++,+- .. =. ",
" - $.. @++!+'+++++++++++++++++@+++@@+,,+ $",
" -@+++&++,+&@,+++++++++++++++++++++ . ",
" $ @,,+@,&+++++&+++++++++++++++++&+%+$ ",
" # =+'+%+@++&&&++++++++++++++++++++++, $",
" #+++@++{++&++++++++++++++++++!+!+@+ ",
" # $ . +++~++!+@+++++++++++++++++&++~,+,",
" ## ; +%+&&++,++++++++++++++++&++%@+++",
" $ ;$ # # +++,+,+++++++++++++++++,&++]+&",
" = $ - @+%+++++++++++++++++++,+&++&+",
" . $ +,++@+@,,++++++++++++++++++",
" $- +&~+%+++@+++++++++++++++++",
" .. .# +++,%,,,@++++++++++++++++",
" $ > ; @@++@+++++++++++++++++++",
" . $ - +%++'%@++++++++++++++++",
" # $# = -%,+@+++++++++++++++++",
" $ .# ..; .$#!+@++++++++++++++++",
" $ # # - +++++++++++++++++"};
3 changes: 3 additions & 0 deletions spec/assets/react.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 42 additions & 4 deletions spec/executeSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,23 @@ export default describe('execute', () => {

it('should support CLI like commands', async done => {
const img1 = await buildInputFile('holocaust.jpg')
const {outputFiles} = await executeOne({inputFiles: [img1], commands: ['convert holocaust.jpg -resize 444x76! output.gif']})
const { outputFiles } = await executeOne({ inputFiles: [img1], commands: ['convert holocaust.jpg -resize 444x76! output.gif'] })
expect(outputFiles[0].name).toBe('output.gif')
const info = await extractInfo(outputFiles[0])
expect(info[0].image.formatDescription.toLowerCase()).toBe('gif')
expect(info[0].image.geometry.width).toBe(444)
expect(info[0].image.geometry.height).toBe(76)
done()
})

it('should return error property and empty outputFiles on error', async done => {
const img = await buildInputFile('fn.png')
const { outputFiles, errors } = await executeOne({ inputFiles: [img], commands: `convert nonexistent.png out.tiff` })
expect(outputFiles.length).toBe(0)
expect(errors).toBeDefined()
expect(errors.length).toBeGreaterThan(0)
done()
})
})

describe('execute', () => {
Expand All @@ -52,7 +61,7 @@ export default describe('execute', () => {
})

it('supports CLI like commands', async done => {
const {outputFiles} = await execute({
const { outputFiles } = await execute({
inputFiles: [await buildInputFile('fn.png', 'image1.png')],
commands: [
'convert image1.png -rotate 70 image2.gif',
Expand All @@ -69,15 +78,44 @@ export default describe('execute', () => {
})

it('supports single string CLI like command', async done => {
const {outputFiles} = await execute({
const { outputFiles } = await execute({
inputFiles: [await buildInputFile('fn.png', 'image1.png')],
commands: 'convert image1.png -rotate 70 image2.gif',
})
expect(outputFiles[0].name).toBe('image2.gif')
done()
})

describe('errors', () => {

it('should return error property and empty outputFiles on error', async done => {
const img = await buildInputFile('fn.png')
const { outputFiles, errors } = await execute({ inputFiles: [img], commands: `convert nonexistent.png out.tiff` })
expect(outputFiles.length).toBe(0)
expect(errors).toBeDefined()
expect(errors.length).toBeGreaterThan(0)
done()
})

it('should return errors per command', async done => {
const img = await buildInputFile('fn.png')
const { outputFiles, errors } = await execute({
inputFiles: [img], commands: [
`convert fn.png out.gif`,
`convert nonexistent.png out.tiff`,
`convert out.gif foo.png`,
],
})
expect(outputFiles.length).toBe(2)
expect(errors.length).toBe(3)
expect(errors.filter(error => !!error).length).toBe(1)
expect(errors[0]).toBeUndefined()
expect(errors[1]).toBeDefined()
expect(errors[2]).toBeUndefined()
done()
})
})
})

xit('event emitter', () => {})
xit('event emitter', () => { })
})
119 changes: 101 additions & 18 deletions spec/formatSpec.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,116 @@
import pmap from 'p-map'
import { buildInputFile, compare } from '../src'
import { buildInputFile, compare, execute, executeOne, extractInfo } from '../src'

export default describe('formats', () => {

const formats = ['jpg', 'png', 'psd', 'tiff', 'xcf', 'gif', 'bmp']
// heads up - all images spec/assets/to_rotate.* where converted using gimp unless explicitly saying otherwhise
const formats = [
'jpg', 'png',
'psd',
'tiff', 'xcf', 'gif', 'bmp', 'tga', 'miff', 'ico', 'dcm', 'xpm', 'pcx',
// 'pix', // gives error
'fits',
// 'djvu', // read only support
'ppm',
'pgm',
'pfm',
'mng',
'hdr',
'dds', // generated using convert -define "dds:compression={dxt1, dxt5, none}" to_rotate.png to_rotate.dds
'otb', // generated using convert to_rotate.png to_rotate.otb

it('compare should be true for all combinations', async done => {
'txt', // generated using convert to_rotate.png to_rotate.txt

const compares = []
formats.forEach(f1 => {
formats.filter(f2 => f2 !== f1).forEach(async f2 => {
compares.push([`to_rotate.${f1}`, `to_rotate.${f2}`])
})
})
const results = await pmap(compares, async c => {
return compare(await buildInputFile(c[0]), await buildInputFile(c[1]))
})
// 'rgb', // fails because MustSpecifyImageSize `to_rotate.rgb'
]

expect(results.length).toBeGreaterThan(formats.length * 2)
const descriptions = {
psd: 'Adobe Photoshop bitmap file'
}

results.forEach((r, i) => {
expect(r).toBe(true, `compare(${compares[i][0]}, ${compares[i][1]})`)
})
describe('compare and assert on info', ()=>{

done()
let jasmineTimeout

beforeAll(() => {
jasmineTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL
jasmine.DEFAULT_TIMEOUT_INTERVAL = 40000
})

afterAll(() => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = jasmineTimeout
})

it('compare should be true for all combinations', async done => {
const compares = []
formats.forEach(f1 => {
formats.filter(f2 => f2 !== f1).forEach(async f2 => {
compares.push([`formats/to_rotate.${f1}`, `formats/to_rotate.${f2}`])
})
})
const results = await pmap(compares, async c => {
return compare(await buildInputFile(c[0]), await buildInputFile(c[1]))
})
expect(results.length).toBeGreaterThan(formats.length * 2)
results.forEach((r, i) => {
expect(r).toBe(true, `compare(${compares[i][0]}, ${compares[i][1]})`)
})
done()
})

it('info format should match', async done => {

const mimeTypes = {
jpg: 'image/jpeg',
png: 'image/png',
tiff: 'image/tiff',
gif: 'image/gif',
ppm: 'image/x-portable-pixmap',
pnm: 'image/x-portable-greymap',
pgm: 'image/x-portable-greymap',
mng: 'video/x-mng',
}
const imFormatMap = {
jpg: 'jpeg',
pnm: 'pgm',
}
const results = await pmap(formats, async f => {
return extractInfo(await buildInputFile(`formats/to_rotate.${f}`))
})
results.forEach((result, i) => {
const image = result[0].image
const format = formats[i]
expect(imFormatMap[format] || format).toBe(image.format.toLowerCase())
expect(image.geometry.width).toBe(64)
expect(image.mimeType).toBe(mimeTypes[format] || undefined)
console.log(image)
})
done()
})

})

xit('extractInfo should get correct format and size', () => {

describe('particular formats not supported or working', ()=>{

it('svg is not supported', async done => {
const img = await buildInputFile('react.svg')
const { outputFiles, errors } = await executeOne({ inputFiles: [img], commands: `convert MSVG:react.svg react.tiff` })
expect(outputFiles.length).toBe(0)
expect(errors).toBeDefined()
expect(errors.length).toBeGreaterThan(0)
done()
})

it('djvu doesn\'t work even read only', async done => {
const img = await buildInputFile('formats/to_rotate.djvu')
const { outputFiles, errors } = await executeOne({ inputFiles: [img], commands: `convert to_rotate.djvu out.tiff` })
// expect(await compare(img, outputFiles[0])).toBe(true)
expect(outputFiles.length).toBe(0)
expect(errors).toBeDefined()
expect(errors.length).toBeGreaterThan(0)
done()
})

})
})
1 change: 0 additions & 1 deletion spec/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ gulp.task('jasmine', function () {

gulp.task('jasmine-server', function () {
return gulp.src(src)
.pipe(watch(src))
.pipe(jasmineBrowser.specRunner({ console: true }))
.pipe(jasmineBrowser.server({ port: 8888 }))
})
21 changes: 15 additions & 6 deletions src/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,22 @@ export type ExecuteCommand = Command[] | string[] | string

export interface ExecuteResult {
outputFiles: MagickOutputFile[]
errors?: any[]
}

/** execute first command in given config */
export async function executeOne(config: ExecuteConfig): Promise<ExecuteResult> {
const command = asCommand(config.commands)[0]
const t0 = performance.now()
executeListeners.forEach(listener => listener.beforeExecute({ command, took: performance.now() - t0, id: t0 }))
const result = { outputFiles: await Call(config.inputFiles, command.map(c => c + '')) }
executeListeners.forEach(listener => listener.afterExecute({ command, took: performance.now() - t0, id: t0 }))
return result
try {
const command = asCommand(config.commands)[0]
const t0 = performance.now()
executeListeners.forEach(listener => listener.beforeExecute({ command, took: performance.now() - t0, id: t0 }))
const result = { outputFiles: await Call(config.inputFiles, command.map(c => c + '')) }
executeListeners.forEach(listener => listener.afterExecute({ command, took: performance.now() - t0, id: t0 }))
return result

} catch (error) {
return { outputFiles: [], errors: [error] }
}
}

// execute event emitter
Expand Down Expand Up @@ -76,6 +82,7 @@ export async function execute(config: ExecuteConfig): Promise<ExecuteResult> {
config.inputFiles.forEach(f => {
allInputFiles[f.name] = f
})
let allErrors = []
async function mapper(c: Command) {
const thisConfig = {
inputFiles: Object.keys(allInputFiles).map(name => allInputFiles[name]),
Expand All @@ -87,10 +94,12 @@ export async function execute(config: ExecuteConfig): Promise<ExecuteResult> {
const inputFile = await outputFileToInputFile(f)
allInputFiles[inputFile.name] = inputFile
})
allErrors = allErrors.concat(result.errors)
}
const commands = asCommand(config.commands)
await pMap(commands, mapper, { concurrency: 1 })
return {
outputFiles: Object.keys(allOutputFiles).map(name => allOutputFiles[name]),
errors: allErrors,
}
}
3 changes: 2 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"extends": "tslint:recommended",
"defaultSeverity": "warning",
"rules": {
"align": [
true,
Expand Down Expand Up @@ -45,6 +46,6 @@
"prefer-for-of": false,
"no-implicit-dependencies": [ true, "dev" ],
"typeof-compare": false,
"no-invalid-template-strings": false
"no-invalid-template-strings": false,
}
}

0 comments on commit bf038b4

Please sign in to comment.