Tests: migrate text suite to async (#4466)

This commit is contained in:
Kleis Auke Wolthuizen 2025-10-27 17:21:37 +01:00 committed by GitHub
parent 9e4e184132
commit 69b2c45615
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -39,7 +39,7 @@ describe('Text to image', function () {
assert.ok(info.textAutofitDpi > 0); assert.ok(info.textAutofitDpi > 0);
}); });
it('text with width and height', function (t, done) { it('text with width and height', async function (t) {
const output = fixtures.path('output.text-width-height.png'); const output = fixtures.path('output.text-width-height.png');
const text = sharp({ const text = sharp({
text: { text: {
@ -51,18 +51,15 @@ describe('Text to image', function () {
if (!sharp.versions.pango) { if (!sharp.versions.pango) {
return t.skip(); return t.skip();
} }
text.toFile(output, function (err, info) { const info = await text.toFile(output);
if (err) throw err; assert.strictEqual('png', info.format);
assert.strictEqual('png', info.format); assert.strictEqual(3, info.channels);
assert.strictEqual(3, info.channels); assert.ok(inRange(info.width, 400, 600), `Actual width ${info.width}`);
assert.ok(inRange(info.width, 400, 600), `Actual width ${info.width}`); assert.ok(inRange(info.height, 290, 500), `Actual height ${info.height}`);
assert.ok(inRange(info.height, 290, 500), `Actual height ${info.height}`); assert.ok(inRange(info.textAutofitDpi, 900, 1300), `Actual textAutofitDpi ${info.textAutofitDpi}`);
assert.ok(inRange(info.textAutofitDpi, 900, 1300), `Actual textAutofitDpi ${info.textAutofitDpi}`);
done();
});
}); });
it('text with dpi', function (t, done) { it('text with dpi', async function (t) {
const output = fixtures.path('output.text-dpi.png'); const output = fixtures.path('output.text-dpi.png');
const dpi = 300; const dpi = 300;
const text = sharp({ const text = sharp({
@ -74,18 +71,13 @@ describe('Text to image', function () {
if (!sharp.versions.pango) { if (!sharp.versions.pango) {
return t.skip(); return t.skip();
} }
text.toFile(output, function (err, info) { const info = await text.toFile(output);
if (err) throw err; assert.strictEqual('png', info.format);
assert.strictEqual('png', info.format); const metadata = await sharp(output).metadata();
sharp(output).metadata(function (err, metadata) { assert.strictEqual(dpi, metadata.density);
if (err) throw err;
assert.strictEqual(dpi, metadata.density);
done();
});
});
}); });
it('text with color and pango markup', function (t, done) { it('text with color and pango markup', async function (t) {
const output = fixtures.path('output.text-color-pango.png'); const output = fixtures.path('output.text-color-pango.png');
const dpi = 300; const dpi = 300;
const text = sharp({ const text = sharp({
@ -98,21 +90,16 @@ describe('Text to image', function () {
if (!sharp.versions.pango) { if (!sharp.versions.pango) {
return t.skip(); return t.skip();
} }
text.toFile(output, function (err, info) { const info = await text.toFile(output);
if (err) throw err; assert.strictEqual('png', info.format);
assert.strictEqual('png', info.format); assert.strictEqual(4, info.channels);
assert.strictEqual(4, info.channels); const metadata = await sharp(output).metadata();
sharp(output).metadata(function (err, metadata) { assert.strictEqual(dpi, metadata.density);
if (err) throw err; assert.strictEqual('uchar', metadata.depth);
assert.strictEqual(dpi, metadata.density); assert.strictEqual(true, metadata.hasAlpha);
assert.strictEqual('uchar', metadata.depth);
assert.strictEqual(true, metadata.hasAlpha);
done();
});
});
}); });
it('text with font', function (t, done) { it('text with font', async function (t) {
const output = fixtures.path('output.text-with-font.png'); const output = fixtures.path('output.text-with-font.png');
const text = sharp({ const text = sharp({
text: { text: {
@ -123,17 +110,14 @@ describe('Text to image', function () {
if (!sharp.versions.pango) { if (!sharp.versions.pango) {
return t.skip(); return t.skip();
} }
text.toFile(output, function (err, info) { const info = await text.toFile(output);
if (err) throw err; assert.strictEqual('png', info.format);
assert.strictEqual('png', info.format); assert.strictEqual(3, info.channels);
assert.strictEqual(3, info.channels); assert.ok(info.width > 30);
assert.ok(info.width > 30); assert.ok(info.height > 10);
assert.ok(info.height > 10);
done();
});
}); });
it('text with justify and composite', function (t, done) { it('text with justify and composite', async function (t) {
const output = fixtures.path('output.text-composite.png'); const output = fixtures.path('output.text-composite.png');
const width = 500; const width = 500;
const dpi = 300; const dpi = 300;
@ -167,20 +151,15 @@ describe('Text to image', function () {
if (!sharp.versions.pango) { if (!sharp.versions.pango) {
return t.skip(); return t.skip();
} }
text.toFile(output, function (err, info) { const info = await text.toFile(output);
if (err) throw err; assert.strictEqual('png', info.format);
assert.strictEqual('png', info.format); assert.strictEqual(4, info.channels);
assert.strictEqual(4, info.channels); assert.strictEqual(width, info.width);
assert.strictEqual(width, info.width); assert.strictEqual(true, info.premultiplied);
assert.strictEqual(true, info.premultiplied); const metadata = await sharp(output).metadata();
sharp(output).metadata(function (err, metadata) { assert.strictEqual('srgb', metadata.space);
if (err) throw err; assert.strictEqual('uchar', metadata.depth);
assert.strictEqual('srgb', metadata.space); assert.strictEqual(true, metadata.hasAlpha);
assert.strictEqual('uchar', metadata.depth);
assert.strictEqual(true, metadata.hasAlpha);
done();
});
});
}); });
it('bad text input', function () { it('bad text input', function () {