2023-12-05 21:18:37 +01:00
|
|
|
const chai = require('chai')
|
|
|
|
const sinon = require('sinon')
|
|
|
|
const fs = require('../../../server/libs/fsExtra')
|
2023-12-14 08:47:18 +01:00
|
|
|
const fileUtils = require('../../../server/utils/fileUtils')
|
2023-12-05 21:18:37 +01:00
|
|
|
const which = require('../../../server/libs/which')
|
|
|
|
const path = require('path')
|
|
|
|
const BinaryManager = require('../../../server/managers/BinaryManager')
|
2024-07-27 21:44:01 +02:00
|
|
|
const { Binary, ffbinaries } = require('../../../server/managers/BinaryManager')
|
2023-12-05 20:19:17 +01:00
|
|
|
|
2023-12-05 21:18:37 +01:00
|
|
|
const expect = chai.expect
|
2023-12-05 20:19:17 +01:00
|
|
|
|
|
|
|
describe('BinaryManager', () => {
|
2023-12-05 21:18:37 +01:00
|
|
|
let binaryManager
|
2023-12-05 20:19:17 +01:00
|
|
|
|
|
|
|
describe('init', () => {
|
2023-12-05 21:18:37 +01:00
|
|
|
let findStub
|
|
|
|
let installStub
|
2024-01-25 16:51:06 +01:00
|
|
|
let removeOldBinariesStub
|
2023-12-05 21:18:37 +01:00
|
|
|
let errorStub
|
|
|
|
let exitStub
|
2023-12-05 20:19:17 +01:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2023-12-05 21:18:37 +01:00
|
|
|
binaryManager = new BinaryManager()
|
|
|
|
findStub = sinon.stub(binaryManager, 'findRequiredBinaries')
|
|
|
|
installStub = sinon.stub(binaryManager, 'install')
|
2024-01-25 16:51:06 +01:00
|
|
|
removeOldBinariesStub = sinon.stub(binaryManager, 'removeOldBinaries')
|
2023-12-05 21:18:37 +01:00
|
|
|
errorStub = sinon.stub(console, 'error')
|
|
|
|
exitStub = sinon.stub(process, 'exit')
|
|
|
|
})
|
2023-12-05 20:19:17 +01:00
|
|
|
|
|
|
|
afterEach(() => {
|
2023-12-05 21:18:37 +01:00
|
|
|
findStub.restore()
|
|
|
|
installStub.restore()
|
2024-01-25 16:51:06 +01:00
|
|
|
removeOldBinariesStub.restore()
|
2023-12-05 21:18:37 +01:00
|
|
|
errorStub.restore()
|
|
|
|
exitStub.restore()
|
|
|
|
})
|
2023-12-05 20:19:17 +01:00
|
|
|
|
|
|
|
it('should not install binaries if they are already found', async () => {
|
2023-12-05 21:18:37 +01:00
|
|
|
findStub.resolves([])
|
2024-07-27 20:51:31 +02:00
|
|
|
|
2023-12-05 21:18:37 +01:00
|
|
|
await binaryManager.init()
|
2023-12-05 20:19:17 +01:00
|
|
|
|
2023-12-05 21:18:37 +01:00
|
|
|
expect(installStub.called).to.be.false
|
2024-01-25 16:51:06 +01:00
|
|
|
expect(removeOldBinariesStub.called).to.be.false
|
2023-12-05 21:18:37 +01:00
|
|
|
expect(findStub.calledOnce).to.be.true
|
|
|
|
expect(errorStub.called).to.be.false
|
|
|
|
expect(exitStub.called).to.be.false
|
|
|
|
})
|
2023-12-05 20:19:17 +01:00
|
|
|
|
|
|
|
it('should install missing binaries', async () => {
|
2024-07-27 20:51:31 +02:00
|
|
|
const ffmpegBinary = new Binary('ffmpeg', 'executable', 'FFMPEG_PATH', ['5.1'], ffbinaries)
|
|
|
|
const ffprobeBinary = new Binary('ffprobe', 'executable', 'FFPROBE_PATH', ['5.1'], ffbinaries)
|
|
|
|
const requiredBinaries = [ffmpegBinary, ffprobeBinary]
|
|
|
|
const missingBinaries = [ffprobeBinary]
|
2023-12-05 21:18:37 +01:00
|
|
|
const missingBinariesAfterInstall = []
|
|
|
|
findStub.onFirstCall().resolves(missingBinaries)
|
|
|
|
findStub.onSecondCall().resolves(missingBinariesAfterInstall)
|
2024-07-27 20:51:31 +02:00
|
|
|
binaryManager.requiredBinaries = requiredBinaries
|
2023-12-05 20:19:17 +01:00
|
|
|
|
2023-12-05 21:18:37 +01:00
|
|
|
await binaryManager.init()
|
2023-12-05 20:19:17 +01:00
|
|
|
|
2023-12-05 21:18:37 +01:00
|
|
|
expect(findStub.calledTwice).to.be.true
|
|
|
|
expect(installStub.calledOnce).to.be.true
|
2024-01-25 16:51:06 +01:00
|
|
|
expect(removeOldBinariesStub.calledOnce).to.be.true
|
2023-12-05 21:18:37 +01:00
|
|
|
expect(errorStub.called).to.be.false
|
|
|
|
expect(exitStub.called).to.be.false
|
|
|
|
})
|
2023-12-05 20:19:17 +01:00
|
|
|
|
|
|
|
it('exit if binaries are not found after installation', async () => {
|
2024-07-27 20:51:31 +02:00
|
|
|
const ffmpegBinary = new Binary('ffmpeg', 'executable', 'FFMPEG_PATH', ['5.1'], ffbinaries)
|
|
|
|
const ffprobeBinary = new Binary('ffprobe', 'executable', 'FFPROBE_PATH', ['5.1'], ffbinaries)
|
|
|
|
const requiredBinaries = [ffmpegBinary, ffprobeBinary]
|
|
|
|
const missingBinaries = [ffprobeBinary]
|
|
|
|
const missingBinariesAfterInstall = [ffprobeBinary]
|
2023-12-05 21:18:37 +01:00
|
|
|
findStub.onFirstCall().resolves(missingBinaries)
|
|
|
|
findStub.onSecondCall().resolves(missingBinariesAfterInstall)
|
2023-12-05 20:19:17 +01:00
|
|
|
|
2023-12-05 21:18:37 +01:00
|
|
|
await binaryManager.init()
|
2023-12-05 20:19:17 +01:00
|
|
|
|
2023-12-05 21:18:37 +01:00
|
|
|
expect(findStub.calledTwice).to.be.true
|
|
|
|
expect(installStub.calledOnce).to.be.true
|
2024-01-25 16:51:06 +01:00
|
|
|
expect(removeOldBinariesStub.calledOnce).to.be.true
|
2023-12-05 21:18:37 +01:00
|
|
|
expect(errorStub.calledOnce).to.be.true
|
|
|
|
expect(exitStub.calledOnce).to.be.true
|
|
|
|
expect(exitStub.calledWith(1)).to.be.true
|
|
|
|
})
|
2024-09-29 08:22:39 +02:00
|
|
|
|
|
|
|
it('should not exit if binaries are not found but not required', async () => {
|
|
|
|
const ffmpegBinary = new Binary('ffmpeg', 'executable', 'FFMPEG_PATH', ['5.1'], ffbinaries)
|
|
|
|
const ffprobeBinary = new Binary('ffprobe', 'executable', 'FFPROBE_PATH', ['5.1'], ffbinaries, false)
|
|
|
|
const requiredBinaries = [ffmpegBinary]
|
|
|
|
const missingBinaries = [ffprobeBinary]
|
|
|
|
const missingBinariesAfterInstall = [ffprobeBinary]
|
|
|
|
findStub.onFirstCall().resolves(missingBinaries)
|
|
|
|
findStub.onSecondCall().resolves(missingBinariesAfterInstall)
|
|
|
|
binaryManager.requiredBinaries = requiredBinaries
|
|
|
|
|
|
|
|
await binaryManager.init()
|
|
|
|
|
|
|
|
expect(findStub.calledTwice).to.be.true
|
|
|
|
expect(installStub.calledOnce).to.be.true
|
|
|
|
expect(removeOldBinariesStub.calledOnce).to.be.true
|
|
|
|
expect(errorStub.called).to.be.false
|
|
|
|
expect(exitStub.called).to.be.false
|
|
|
|
})
|
2023-12-05 21:18:37 +01:00
|
|
|
})
|
2023-12-05 20:19:17 +01:00
|
|
|
|
|
|
|
describe('findRequiredBinaries', () => {
|
2023-12-05 21:18:37 +01:00
|
|
|
let findBinaryStub
|
2024-07-27 20:51:31 +02:00
|
|
|
let ffmpegBinary
|
2023-12-05 20:19:17 +01:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2024-07-27 20:51:31 +02:00
|
|
|
ffmpegBinary = new Binary('ffmpeg', 'executable', 'FFMPEG_PATH', ['5.1'], ffbinaries)
|
|
|
|
const requiredBinaries = [ffmpegBinary]
|
2023-12-05 21:18:37 +01:00
|
|
|
binaryManager = new BinaryManager(requiredBinaries)
|
2024-07-27 20:51:31 +02:00
|
|
|
findBinaryStub = sinon.stub(ffmpegBinary, 'find')
|
2023-12-05 21:18:37 +01:00
|
|
|
})
|
2023-12-05 20:19:17 +01:00
|
|
|
|
|
|
|
afterEach(() => {
|
2023-12-05 21:18:37 +01:00
|
|
|
findBinaryStub.restore()
|
|
|
|
})
|
2023-12-05 20:19:17 +01:00
|
|
|
|
|
|
|
it('should put found paths in the correct environment variables', async () => {
|
2023-12-05 21:18:37 +01:00
|
|
|
const pathToFFmpeg = '/path/to/ffmpeg'
|
|
|
|
const missingBinaries = []
|
|
|
|
delete process.env.FFMPEG_PATH
|
|
|
|
findBinaryStub.resolves(pathToFFmpeg)
|
2023-12-05 20:19:17 +01:00
|
|
|
|
2023-12-05 21:18:37 +01:00
|
|
|
const result = await binaryManager.findRequiredBinaries()
|
2023-12-05 20:19:17 +01:00
|
|
|
|
2023-12-05 21:18:37 +01:00
|
|
|
expect(result).to.deep.equal(missingBinaries)
|
|
|
|
expect(findBinaryStub.calledOnce).to.be.true
|
|
|
|
expect(process.env.FFMPEG_PATH).to.equal(pathToFFmpeg)
|
|
|
|
})
|
2023-12-05 20:19:17 +01:00
|
|
|
|
|
|
|
it('should add missing binaries to result', async () => {
|
2024-07-27 20:51:31 +02:00
|
|
|
const missingBinaries = [ffmpegBinary]
|
|
|
|
delete process.env.FFMPEG_PATH
|
2023-12-05 21:18:37 +01:00
|
|
|
findBinaryStub.resolves(null)
|
2023-12-05 20:19:17 +01:00
|
|
|
|
2023-12-05 21:18:37 +01:00
|
|
|
const result = await binaryManager.findRequiredBinaries()
|
2023-12-05 20:19:17 +01:00
|
|
|
|
2023-12-05 21:18:37 +01:00
|
|
|
expect(result).to.deep.equal(missingBinaries)
|
|
|
|
expect(findBinaryStub.calledOnce).to.be.true
|
|
|
|
expect(process.env.FFMPEG_PATH).to.be.undefined
|
|
|
|
})
|
|
|
|
})
|
2024-07-27 20:51:31 +02:00
|
|
|
|
2023-12-05 20:19:17 +01:00
|
|
|
describe('install', () => {
|
2023-12-14 08:47:18 +01:00
|
|
|
let isWritableStub
|
2024-07-27 20:51:31 +02:00
|
|
|
let downloadBinaryStub
|
|
|
|
let ffmpegBinary
|
2023-12-05 20:19:17 +01:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2024-07-27 20:51:31 +02:00
|
|
|
ffmpegBinary = new Binary('ffmpeg', 'executable', 'FFMPEG_PATH', ['5.1'], ffbinaries)
|
|
|
|
const requiredBinaries = [ffmpegBinary]
|
|
|
|
binaryManager = new BinaryManager(requiredBinaries)
|
2023-12-14 08:47:18 +01:00
|
|
|
isWritableStub = sinon.stub(fileUtils, 'isWritable')
|
2024-07-27 20:51:31 +02:00
|
|
|
downloadBinaryStub = sinon.stub(ffmpegBinary, 'download')
|
|
|
|
binaryManager.mainInstallDir = '/path/to/main/install'
|
|
|
|
binaryManager.altInstallDir = '/path/to/alt/install'
|
2023-12-05 21:18:37 +01:00
|
|
|
})
|
2023-12-05 20:19:17 +01:00
|
|
|
|
|
|
|
afterEach(() => {
|
2023-12-14 08:47:18 +01:00
|
|
|
isWritableStub.restore()
|
2024-07-27 20:51:31 +02:00
|
|
|
downloadBinaryStub.restore()
|
2023-12-05 21:18:37 +01:00
|
|
|
})
|
2023-12-05 20:19:17 +01:00
|
|
|
|
|
|
|
it('should not install binaries if no binaries are passed', async () => {
|
2023-12-05 21:18:37 +01:00
|
|
|
const binaries = []
|
2023-12-05 20:19:17 +01:00
|
|
|
|
2023-12-05 21:18:37 +01:00
|
|
|
await binaryManager.install(binaries)
|
2023-12-05 20:19:17 +01:00
|
|
|
|
2023-12-14 08:47:18 +01:00
|
|
|
expect(isWritableStub.called).to.be.false
|
2024-07-27 20:51:31 +02:00
|
|
|
expect(downloadBinaryStub.called).to.be.false
|
2023-12-05 21:18:37 +01:00
|
|
|
})
|
2023-12-05 20:19:17 +01:00
|
|
|
|
|
|
|
it('should install binaries in main install path if has access', async () => {
|
2024-07-27 20:51:31 +02:00
|
|
|
const binaries = [ffmpegBinary]
|
|
|
|
const destination = binaryManager.mainInstallDir
|
2023-12-14 08:47:18 +01:00
|
|
|
isWritableStub.withArgs(destination).resolves(true)
|
2024-07-27 20:51:31 +02:00
|
|
|
downloadBinaryStub.resolves()
|
|
|
|
|
2023-12-05 21:18:37 +01:00
|
|
|
await binaryManager.install(binaries)
|
2023-12-05 20:19:17 +01:00
|
|
|
|
2023-12-14 08:47:18 +01:00
|
|
|
expect(isWritableStub.calledOnce).to.be.true
|
2024-07-27 20:51:31 +02:00
|
|
|
expect(downloadBinaryStub.calledOnce).to.be.true
|
|
|
|
expect(downloadBinaryStub.calledWith(destination)).to.be.true
|
2023-12-05 21:18:37 +01:00
|
|
|
})
|
2023-12-05 20:19:17 +01:00
|
|
|
|
|
|
|
it('should install binaries in alt install path if has no access to main', async () => {
|
2024-07-27 20:51:31 +02:00
|
|
|
const binaries = [ffmpegBinary]
|
|
|
|
const mainDestination = binaryManager.mainInstallDir
|
|
|
|
const destination = binaryManager.altInstallDir
|
2023-12-14 08:47:18 +01:00
|
|
|
isWritableStub.withArgs(mainDestination).resolves(false)
|
2024-07-27 20:51:31 +02:00
|
|
|
downloadBinaryStub.resolves()
|
|
|
|
|
2023-12-05 21:18:37 +01:00
|
|
|
await binaryManager.install(binaries)
|
2023-12-05 20:19:17 +01:00
|
|
|
|
2023-12-14 08:47:18 +01:00
|
|
|
expect(isWritableStub.calledOnce).to.be.true
|
2024-07-27 20:51:31 +02:00
|
|
|
expect(downloadBinaryStub.calledOnce).to.be.true
|
|
|
|
expect(downloadBinaryStub.calledWith(destination)).to.be.true
|
2023-12-05 21:18:37 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2023-12-05 20:19:17 +01:00
|
|
|
|
2024-07-27 20:51:31 +02:00
|
|
|
describe('Binary', () => {
|
|
|
|
describe('find', () => {
|
|
|
|
let binary
|
|
|
|
let isGoodStub
|
|
|
|
let whichSyncStub
|
|
|
|
let mainInstallPath
|
|
|
|
let altInstallPath
|
2023-12-05 20:19:17 +01:00
|
|
|
|
2024-07-27 20:51:31 +02:00
|
|
|
const name = 'ffmpeg'
|
|
|
|
const envVariable = 'FFMPEG_PATH'
|
|
|
|
const defaultPath = '/path/to/ffmpeg'
|
|
|
|
const executable = name + (process.platform == 'win32' ? '.exe' : '')
|
|
|
|
const whichPath = '/usr/bin/ffmpeg'
|
2024-01-25 16:51:06 +01:00
|
|
|
|
2024-07-27 20:51:31 +02:00
|
|
|
beforeEach(() => {
|
|
|
|
binary = new Binary(name, 'executable', envVariable, ['5.1'], ffbinaries)
|
|
|
|
isGoodStub = sinon.stub(binary, 'isGood')
|
|
|
|
whichSyncStub = sinon.stub(which, 'sync')
|
|
|
|
binary.mainInstallDir = '/path/to/main/install'
|
|
|
|
mainInstallPath = path.join(binary.mainInstallDir, executable)
|
|
|
|
binary.altInstallDir = '/path/to/alt/install'
|
|
|
|
altInstallPath = path.join(binary.altInstallDir, executable)
|
|
|
|
})
|
2024-01-25 16:51:06 +01:00
|
|
|
|
2024-07-27 20:51:31 +02:00
|
|
|
afterEach(() => {
|
|
|
|
isGoodStub.restore()
|
|
|
|
whichSyncStub.restore()
|
|
|
|
})
|
2024-01-25 16:51:06 +01:00
|
|
|
|
2024-07-27 20:51:31 +02:00
|
|
|
it('should return the defaultPath if it exists and is a good binary', async () => {
|
|
|
|
process.env[envVariable] = defaultPath
|
|
|
|
isGoodStub.withArgs(defaultPath).resolves(true)
|
2024-01-25 16:51:06 +01:00
|
|
|
|
2024-07-27 20:51:31 +02:00
|
|
|
const result = await binary.find(binary.mainInstallDir, binary.altInstallDir)
|
2024-01-25 16:51:06 +01:00
|
|
|
|
2024-07-27 20:51:31 +02:00
|
|
|
expect(result).to.equal(defaultPath)
|
|
|
|
expect(isGoodStub.calledOnce).to.be.true
|
|
|
|
expect(isGoodStub.calledWith(defaultPath)).to.be.true
|
|
|
|
})
|
2024-01-25 16:51:06 +01:00
|
|
|
|
2024-07-27 20:51:31 +02:00
|
|
|
it('should return the whichPath if it exists and is a good binary', async () => {
|
|
|
|
delete process.env[envVariable]
|
|
|
|
isGoodStub.withArgs(undefined).resolves(false)
|
|
|
|
whichSyncStub.returns(whichPath)
|
|
|
|
isGoodStub.withArgs(whichPath).resolves(true)
|
2024-01-25 16:51:06 +01:00
|
|
|
|
2024-07-27 20:51:31 +02:00
|
|
|
const result = await binary.find(binary.mainInstallDir, binary.altInstallDir)
|
2024-01-25 16:51:06 +01:00
|
|
|
|
2024-07-27 20:51:31 +02:00
|
|
|
expect(result).to.equal(whichPath)
|
|
|
|
expect(isGoodStub.calledTwice).to.be.true
|
|
|
|
expect(isGoodStub.calledWith(undefined)).to.be.true
|
|
|
|
expect(isGoodStub.calledWith(whichPath)).to.be.true
|
|
|
|
})
|
2024-01-25 16:51:06 +01:00
|
|
|
|
2024-07-27 20:51:31 +02:00
|
|
|
it('should return the mainInstallPath if it exists and is a good binary', async () => {
|
|
|
|
delete process.env[envVariable]
|
|
|
|
isGoodStub.withArgs(undefined).resolves(false)
|
|
|
|
whichSyncStub.returns(null)
|
|
|
|
isGoodStub.withArgs(null).resolves(false)
|
|
|
|
isGoodStub.withArgs(mainInstallPath).resolves(true)
|
2024-01-25 16:51:06 +01:00
|
|
|
|
2024-07-27 20:51:31 +02:00
|
|
|
const result = await binary.find(binary.mainInstallDir, binary.altInstallDir)
|
2024-01-25 16:51:06 +01:00
|
|
|
|
2024-07-27 20:51:31 +02:00
|
|
|
expect(result).to.equal(mainInstallPath)
|
|
|
|
expect(isGoodStub.callCount).to.be.equal(3)
|
|
|
|
expect(isGoodStub.calledWith(undefined)).to.be.true
|
|
|
|
expect(isGoodStub.calledWith(null)).to.be.true
|
|
|
|
expect(isGoodStub.calledWith(mainInstallPath)).to.be.true
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return the altInstallPath if it exists and is a good binary', async () => {
|
|
|
|
delete process.env[envVariable]
|
|
|
|
isGoodStub.withArgs(undefined).resolves(false)
|
|
|
|
whichSyncStub.returns(null)
|
|
|
|
isGoodStub.withArgs(null).resolves(false)
|
|
|
|
isGoodStub.withArgs(mainInstallPath).resolves(false)
|
|
|
|
isGoodStub.withArgs(altInstallPath).resolves(true)
|
|
|
|
|
|
|
|
const result = await binary.find(binary.mainInstallDir, binary.altInstallDir)
|
|
|
|
|
|
|
|
expect(result).to.equal(altInstallPath)
|
|
|
|
expect(isGoodStub.callCount).to.be.equal(4)
|
|
|
|
expect(isGoodStub.calledWith(undefined)).to.be.true
|
|
|
|
expect(isGoodStub.calledWith(null)).to.be.true
|
|
|
|
expect(isGoodStub.calledWith(mainInstallPath)).to.be.true
|
|
|
|
expect(isGoodStub.calledWith(altInstallPath)).to.be.true
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return null if no good binary is found', async () => {
|
|
|
|
delete process.env[envVariable]
|
|
|
|
isGoodStub.withArgs(undefined).resolves(false)
|
|
|
|
whichSyncStub.returns(null)
|
|
|
|
isGoodStub.withArgs(null).resolves(false)
|
|
|
|
isGoodStub.withArgs(mainInstallPath).resolves(false)
|
|
|
|
isGoodStub.withArgs(altInstallPath).resolves(false)
|
|
|
|
|
|
|
|
const result = await binary.find(binary.mainInstallDir, binary.altInstallDir)
|
|
|
|
|
|
|
|
expect(result).to.be.null
|
|
|
|
expect(isGoodStub.callCount).to.be.equal(4)
|
|
|
|
expect(isGoodStub.calledWith(undefined)).to.be.true
|
|
|
|
expect(isGoodStub.calledWith(null)).to.be.true
|
|
|
|
expect(isGoodStub.calledWith(mainInstallPath)).to.be.true
|
|
|
|
expect(isGoodStub.calledWith(altInstallPath)).to.be.true
|
|
|
|
})
|
2024-01-25 16:51:06 +01:00
|
|
|
})
|
|
|
|
|
2024-07-27 20:51:31 +02:00
|
|
|
describe('isGood', () => {
|
|
|
|
let binary
|
|
|
|
let fsPathExistsStub
|
2024-09-29 08:22:39 +02:00
|
|
|
let fsReadFileStub
|
2024-07-27 20:51:31 +02:00
|
|
|
let execStub
|
2024-01-25 16:51:06 +01:00
|
|
|
|
2024-07-27 20:51:31 +02:00
|
|
|
const binaryPath = '/path/to/binary'
|
|
|
|
const execCommand = '"' + binaryPath + '"' + ' -version'
|
|
|
|
const goodVersions = ['5.1', '6']
|
2024-01-25 16:51:06 +01:00
|
|
|
|
2024-07-27 20:51:31 +02:00
|
|
|
beforeEach(() => {
|
|
|
|
binary = new Binary('ffmpeg', 'executable', 'FFMPEG_PATH', goodVersions, ffbinaries)
|
|
|
|
fsPathExistsStub = sinon.stub(fs, 'pathExists')
|
2024-09-29 08:22:39 +02:00
|
|
|
fsReadFileStub = sinon.stub(fs, 'readFile')
|
2024-07-27 20:51:31 +02:00
|
|
|
execStub = sinon.stub(binary, 'exec')
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
fsPathExistsStub.restore()
|
2024-09-29 08:22:39 +02:00
|
|
|
fsReadFileStub.restore()
|
2024-07-27 20:51:31 +02:00
|
|
|
execStub.restore()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return false if binaryPath is falsy', async () => {
|
|
|
|
fsPathExistsStub.resolves(true)
|
|
|
|
|
|
|
|
const result = await binary.isGood(null)
|
|
|
|
|
|
|
|
expect(result).to.be.false
|
|
|
|
expect(fsPathExistsStub.called).to.be.false
|
|
|
|
expect(execStub.called).to.be.false
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return false if binaryPath does not exist', async () => {
|
|
|
|
fsPathExistsStub.resolves(false)
|
2024-01-25 16:51:06 +01:00
|
|
|
|
2024-07-27 20:51:31 +02:00
|
|
|
const result = await binary.isGood(binaryPath)
|
2024-01-25 16:51:06 +01:00
|
|
|
|
2024-07-27 20:51:31 +02:00
|
|
|
expect(result).to.be.false
|
|
|
|
expect(fsPathExistsStub.calledOnce).to.be.true
|
|
|
|
expect(fsPathExistsStub.calledWith(binaryPath)).to.be.true
|
|
|
|
expect(execStub.called).to.be.false
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return false if failed to check version of binary', async () => {
|
|
|
|
fsPathExistsStub.resolves(true)
|
|
|
|
execStub.rejects(new Error('Failed to execute command'))
|
|
|
|
|
|
|
|
const result = await binary.isGood(binaryPath)
|
|
|
|
|
|
|
|
expect(result).to.be.false
|
|
|
|
expect(fsPathExistsStub.calledOnce).to.be.true
|
|
|
|
expect(fsPathExistsStub.calledWith(binaryPath)).to.be.true
|
|
|
|
expect(execStub.calledOnce).to.be.true
|
|
|
|
expect(execStub.calledWith(execCommand)).to.be.true
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return false if version is not found', async () => {
|
|
|
|
const stdout = 'Some output without version'
|
|
|
|
fsPathExistsStub.resolves(true)
|
|
|
|
execStub.resolves({ stdout })
|
|
|
|
|
|
|
|
const result = await binary.isGood(binaryPath)
|
|
|
|
|
|
|
|
expect(result).to.be.false
|
|
|
|
expect(fsPathExistsStub.calledOnce).to.be.true
|
|
|
|
expect(fsPathExistsStub.calledWith(binaryPath)).to.be.true
|
|
|
|
expect(execStub.calledOnce).to.be.true
|
|
|
|
expect(execStub.calledWith(execCommand)).to.be.true
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return false if version is found but does not match a good version', async () => {
|
|
|
|
const stdout = 'version 1.2.3'
|
|
|
|
fsPathExistsStub.resolves(true)
|
|
|
|
execStub.resolves({ stdout })
|
|
|
|
|
|
|
|
const result = await binary.isGood(binaryPath)
|
2024-01-25 16:51:06 +01:00
|
|
|
|
2024-07-27 20:51:31 +02:00
|
|
|
expect(result).to.be.false
|
|
|
|
expect(fsPathExistsStub.calledOnce).to.be.true
|
|
|
|
expect(fsPathExistsStub.calledWith(binaryPath)).to.be.true
|
|
|
|
expect(execStub.calledOnce).to.be.true
|
|
|
|
expect(execStub.calledWith(execCommand)).to.be.true
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return true if version is found and matches a good version', async () => {
|
|
|
|
const stdout = 'version 6.1.2'
|
|
|
|
fsPathExistsStub.resolves(true)
|
|
|
|
execStub.resolves({ stdout })
|
|
|
|
|
|
|
|
const result = await binary.isGood(binaryPath)
|
|
|
|
|
|
|
|
expect(result).to.be.true
|
|
|
|
expect(fsPathExistsStub.calledOnce).to.be.true
|
|
|
|
expect(fsPathExistsStub.calledWith(binaryPath)).to.be.true
|
|
|
|
expect(execStub.calledOnce).to.be.true
|
|
|
|
expect(execStub.calledWith(execCommand)).to.be.true
|
|
|
|
})
|
2024-09-29 08:22:39 +02:00
|
|
|
|
|
|
|
it('should check library version file', async () => {
|
|
|
|
const binary = new Binary('libavcodec', 'library', 'FFMPEG_PATH', ['5.1'], ffbinaries)
|
|
|
|
fsReadFileStub.resolves('5.1.2 ')
|
|
|
|
fsPathExistsStub.onFirstCall().resolves(true)
|
|
|
|
fsPathExistsStub.onSecondCall().resolves(true)
|
|
|
|
|
|
|
|
const result = await binary.isGood(binaryPath)
|
|
|
|
|
|
|
|
expect(result).to.be.true
|
|
|
|
expect(fsPathExistsStub.calledTwice).to.be.true
|
|
|
|
expect(fsPathExistsStub.firstCall.args[0]).to.be.equal(binaryPath)
|
|
|
|
expect(fsPathExistsStub.secondCall.args[0]).to.be.equal(binaryPath + '.ver')
|
|
|
|
expect(fsReadFileStub.calledOnce).to.be.true
|
|
|
|
expect(fsReadFileStub.calledWith(binaryPath + '.ver'), 'utf8').to.be.true
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return false if library version file does not exist', async () => {
|
|
|
|
const binary = new Binary('libavcodec', 'library', 'FFMPEG_PATH', ['5.1'], ffbinaries)
|
|
|
|
fsReadFileStub.resolves('5.1.2 ')
|
|
|
|
fsPathExistsStub.onFirstCall().resolves(true)
|
|
|
|
fsPathExistsStub.onSecondCall().resolves(false)
|
|
|
|
|
|
|
|
const result = await binary.isGood(binaryPath)
|
|
|
|
|
|
|
|
expect(result).to.be.false
|
|
|
|
expect(fsPathExistsStub.calledTwice).to.be.true
|
|
|
|
expect(fsPathExistsStub.firstCall.args[0]).to.be.equal(binaryPath)
|
|
|
|
expect(fsPathExistsStub.secondCall.args[0]).to.be.equal(binaryPath + '.ver')
|
|
|
|
expect(fsReadFileStub.called).to.be.false
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return false if library version does not match a valid version', async () => {
|
|
|
|
const binary = new Binary('libavcodec', 'library', 'FFMPEG_PATH', ['5.1'], ffbinaries)
|
|
|
|
fsReadFileStub.resolves('5.2.1 ')
|
|
|
|
fsPathExistsStub.onFirstCall().resolves(true)
|
|
|
|
fsPathExistsStub.onSecondCall().resolves(true)
|
|
|
|
|
|
|
|
const result = await binary.isGood(binaryPath)
|
|
|
|
|
|
|
|
expect(result).to.be.false
|
|
|
|
expect(fsPathExistsStub.calledTwice).to.be.true
|
|
|
|
expect(fsPathExistsStub.firstCall.args[0]).to.be.equal(binaryPath)
|
|
|
|
expect(fsPathExistsStub.secondCall.args[0]).to.be.equal(binaryPath + '.ver')
|
|
|
|
expect(fsReadFileStub.calledOnce).to.be.true
|
|
|
|
expect(fsReadFileStub.calledWith(binaryPath + '.ver'), 'utf8').to.be.true
|
|
|
|
})
|
2024-01-25 16:51:06 +01:00
|
|
|
})
|
|
|
|
|
2024-07-27 20:51:31 +02:00
|
|
|
describe('getFileName', () => {
|
|
|
|
let originalPlatform
|
|
|
|
|
|
|
|
const mockPlatform = (platform) => {
|
|
|
|
Object.defineProperty(process, 'platform', { value: platform })
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
// Save the original process.platform descriptor
|
|
|
|
originalPlatform = Object.getOwnPropertyDescriptor(process, 'platform')
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
// Restore the original process.platform descriptor
|
|
|
|
Object.defineProperty(process, 'platform', originalPlatform)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return the executable file name with .exe extension on Windows', () => {
|
|
|
|
const binary = new Binary('ffmpeg', 'executable', 'FFMPEG_PATH', ['5.1'], ffbinaries)
|
|
|
|
mockPlatform('win32')
|
|
|
|
|
|
|
|
const result = binary.getFileName()
|
|
|
|
|
|
|
|
expect(result).to.equal('ffmpeg.exe')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return the executable file name without extension on linux', () => {
|
|
|
|
const binary = new Binary('ffmpeg', 'executable', 'FFMPEG_PATH', ['5.1'], ffbinaries)
|
|
|
|
mockPlatform('linux')
|
|
|
|
|
|
|
|
const result = binary.getFileName()
|
|
|
|
|
|
|
|
expect(result).to.equal('ffmpeg')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return the library file name with .dll extension on Windows', () => {
|
|
|
|
const binary = new Binary('ffmpeg', 'library', 'FFMPEG_PATH', ['5.1'], ffbinaries)
|
|
|
|
mockPlatform('win32')
|
2024-01-25 16:51:06 +01:00
|
|
|
|
2024-07-27 20:51:31 +02:00
|
|
|
const result = binary.getFileName()
|
2024-01-25 16:51:06 +01:00
|
|
|
|
2024-07-27 20:51:31 +02:00
|
|
|
expect(result).to.equal('ffmpeg.dll')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return the library file name with .so extension on linux', () => {
|
|
|
|
const binary = new Binary('ffmpeg', 'library', 'FFMPEG_PATH', ['5.1'], ffbinaries)
|
|
|
|
mockPlatform('linux')
|
|
|
|
|
|
|
|
const result = binary.getFileName()
|
|
|
|
|
|
|
|
expect(result).to.equal('ffmpeg.so')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should return the file name without extension for other types', () => {
|
|
|
|
const binary = new Binary('ffmpeg', 'other', 'FFMPEG_PATH', ['5.1'], ffbinaries)
|
|
|
|
mockPlatform('win32')
|
|
|
|
|
|
|
|
const result = binary.getFileName()
|
|
|
|
|
|
|
|
expect(result).to.equal('ffmpeg')
|
|
|
|
})
|
2023-12-05 21:18:37 +01:00
|
|
|
})
|
2024-09-29 08:22:39 +02:00
|
|
|
|
|
|
|
describe('download', () => {
|
|
|
|
let binary
|
|
|
|
let downloadBinaryStub
|
|
|
|
let fsWriteFileStub
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
binary = new Binary('ffmpeg', 'executable', 'FFMPEG_PATH', ['5.1'], ffbinaries)
|
|
|
|
downloadBinaryStub = sinon.stub(binary.source, 'downloadBinary')
|
|
|
|
fsWriteFileStub = sinon.stub(fs, 'writeFile')
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
downloadBinaryStub.restore()
|
|
|
|
fsWriteFileStub.restore()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should call downloadBinary with the correct parameters', async () => {
|
|
|
|
const destination = '/path/to/destination'
|
|
|
|
|
|
|
|
await binary.download(destination)
|
|
|
|
|
|
|
|
expect(downloadBinaryStub.calledOnce).to.be.true
|
|
|
|
expect(downloadBinaryStub.calledWith('ffmpeg', '5.1', destination)).to.be.true
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should write a version file for libraries', async () => {
|
|
|
|
const binary = new Binary('libavcodec', 'library', 'FFMPEG_PATH', ['5.1'], ffbinaries)
|
|
|
|
const destination = '/path/to/destination'
|
|
|
|
const versionFilePath = path.join(destination, binary.fileName) + '.ver'
|
|
|
|
|
|
|
|
await binary.download(destination)
|
|
|
|
|
|
|
|
expect(downloadBinaryStub.calledOnce).to.be.true
|
|
|
|
expect(downloadBinaryStub.calledWith('libavcodec', '5.1', destination)).to.be.true
|
|
|
|
expect(fsWriteFileStub.calledOnce).to.be.true
|
|
|
|
expect(fsWriteFileStub.calledWith(versionFilePath, '5.1')).to.be.true
|
|
|
|
})
|
|
|
|
})
|
2024-07-27 20:51:31 +02:00
|
|
|
})
|