Avoid loading 'mmmagic' on Linux (fix "could not load any valid magic files")

Resolves: #1596
Change-type: patch
This commit is contained in:
Paulo Castro 2020-02-05 22:26:55 +00:00
parent da3c11533c
commit 4502f2a203
4 changed files with 81 additions and 37 deletions

View File

@ -14,11 +14,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import mmmagic = require('mmmagic');
import fs = require('mz/fs');
import Logger = require('./logger');
const globalLogger = Logger.getLogger();
// Define file size threshold (bytes) over which analysis/conversion is not performed.
const LARGE_FILE_THRESHOLD = 10 * 1000 * 1000;
@ -31,6 +26,7 @@ const CONVERTIBLE_ENCODINGS = ['ascii', 'utf-8'];
* @param data
*/
async function detectEncoding(data: Buffer): Promise<string> {
const mmmagic = await import('mmmagic');
// Instantiate mmmagic for mime encoding analysis
const magic = new mmmagic.Magic(mmmagic.MAGIC_MIME_ENCODING);
@ -94,8 +90,16 @@ export async function readFileWithEolConversion(
filepath: string,
convertEol: boolean,
): Promise<Buffer> {
const { fs } = await import('mz');
const fileBuffer = await fs.readFile(filepath);
if (process.platform !== 'win32') {
return fileBuffer;
}
const Logger = await import('./logger');
const globalLogger = Logger.getLogger();
// Skip processing of very large files
const fileStats = await fs.stat(filepath);
if (fileStats.size > LARGE_FILE_THRESHOLD) {

View File

@ -105,28 +105,37 @@ describe('balena build', function() {
`build ${projectPath} --deviceType nuc --arch amd64`,
);
const extraLines = [
`[Info] Creating default composition with source: ${projectPath}`,
];
if (process.platform === 'win32') {
extraLines.push(
`[Warn] CRLF (Windows) line endings detected in file: ${path.join(
projectPath,
'src',
'windows-crlf.sh',
)}`,
);
}
expect(err).to.have.members([]);
expect(
cleanOutput(out).map(line => line.replace(/\s{2,}/g, ' ')),
).to.include.members([
`[Info] Creating default composition with source: ${projectPath}`,
...expectedResponses[responseFilename],
`[Warn] CRLF (Windows) line endings detected in file: ${path.join(
projectPath,
'src',
'windows-crlf.sh',
)}`,
...extraLines,
]);
});
it('should create the expected tar stream (single container, --convert-eol)', async () => {
const windows = process.platform === 'win32';
const projectPath = path.join(projectsPath, 'no-docker-compose', 'basic');
const expectedFiles: TarStreamFiles = {
'src/start.sh': { fileSize: 89, type: 'file' },
'src/windows-crlf.sh': {
fileSize: 68,
fileSize: windows ? 68 : 70,
type: 'file',
testStream: expectStreamNoCRLF,
testStream: windows ? expectStreamNoCRLF : undefined,
},
Dockerfile: { fileSize: 88, type: 'file' },
'Dockerfile-alt': { fileSize: 30, type: 'file' },
@ -154,17 +163,25 @@ describe('balena build', function() {
`build ${projectPath} --deviceType nuc --arch amd64 --convert-eol`,
);
const extraLines = [
`[Info] Creating default composition with source: ${projectPath}`,
];
if (windows) {
extraLines.push(
`[Info] Converting line endings CRLF -> LF for file: ${path.join(
projectPath,
'src',
'windows-crlf.sh',
)}`,
);
}
expect(err).to.have.members([]);
expect(
cleanOutput(out).map(line => line.replace(/\s{2,}/g, ' ')),
).to.include.members([
`[Info] Creating default composition with source: ${projectPath}`,
`[Info] Converting line endings CRLF -> LF for file: ${path.join(
projectPath,
'src',
'windows-crlf.sh',
)}`,
...expectedResponses[responseFilename],
...extraLines,
]);
});
});

View File

@ -124,17 +124,25 @@ describe('balena deploy', function() {
`deploy testApp --build --source ${projectPath}`,
);
const extraLines = [
`[Info] Creating default composition with source: ${projectPath}`,
];
if (process.platform === 'win32') {
extraLines.push(
`[Warn] CRLF (Windows) line endings detected in file: ${path.join(
projectPath,
'src',
'windows-crlf.sh',
)}`,
);
}
expect(err).to.have.members([]);
expect(
cleanOutput(out).map(line => line.replace(/\s{2,}/g, ' ')),
).to.include.members([
`[Info] Creating default composition with source: ${projectPath}`,
...expectedResponses[responseFilename],
`[Warn] CRLF (Windows) line endings detected in file: ${path.join(
projectPath,
'src',
'windows-crlf.sh',
)}`,
...extraLines,
]);
});
});

View File

@ -133,14 +133,21 @@ describe('balena push', function() {
`push testApp --source ${projectPath}`,
);
const extraLines = [];
if (process.platform === 'win32') {
extraLines.push(
`[Warn] CRLF (Windows) line endings detected in file: ${path.join(
projectPath,
'src',
'windows-crlf.sh',
)}`,
);
}
expect(err).to.have.members([]);
expect(tweakOutput(out)).to.include.members([
...expectedResponses[responseFilename],
`[Warn] CRLF (Windows) line endings detected in file: ${path.join(
projectPath,
'src',
'windows-crlf.sh',
)}`,
...extraLines,
]);
});
@ -187,13 +194,14 @@ describe('balena push', function() {
});
it('should create the expected tar stream (single container, --convert-eol)', async () => {
const windows = process.platform === 'win32';
const projectPath = path.join(projectsPath, 'no-docker-compose', 'basic');
const expectedFiles: TarStreamFiles = {
'src/start.sh': { fileSize: 89, type: 'file' },
'src/windows-crlf.sh': {
fileSize: 68,
fileSize: windows ? 68 : 70,
type: 'file',
testStream: expectStreamNoCRLF,
testStream: windows ? expectStreamNoCRLF : undefined,
},
Dockerfile: { fileSize: 88, type: 'file' },
'Dockerfile-alt': { fileSize: 30, type: 'file' },
@ -220,14 +228,21 @@ describe('balena push', function() {
`push testApp --source ${projectPath} --convert-eol`,
);
const extraLines = [];
if (windows) {
extraLines.push(
`[Info] Converting line endings CRLF -> LF for file: ${path.join(
projectPath,
'src',
'windows-crlf.sh',
)}`,
);
}
expect(err).to.have.members([]);
expect(tweakOutput(out)).to.include.members([
...expectedResponses[responseFilename],
`[Info] Converting line endings CRLF -> LF for file: ${path.join(
projectPath,
'src',
'windows-crlf.sh',
)}`,
...extraLines,
]);
});
});