Skip to content

Commit e1f94fb

Browse files
authored
chore(action): add test coverage (#721)
1 parent 96c85e2 commit e1f94fb

File tree

6 files changed

+544
-119
lines changed

6 files changed

+544
-119
lines changed

action/dist/main.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83310,6 +83310,12 @@ var PutObjectCommand = class extends Command2.classBuilder().ep({
8331083310
}).s("AmazonS3", "PutObject", {}).n("S3Client", "PutObjectCommand").sc(PutObject$).build() {
8331183311
};
8331283312

83313+
// src/s3-client.ts
83314+
var s3Client = new S3Client();
83315+
var listObjects = (input) => s3Client.send(new ListObjectsV2Command(input));
83316+
var getObject = (input) => s3Client.send(new GetObjectCommand(input));
83317+
var putObject = (input) => s3Client.send(new PutObjectCommand(input));
83318+
8331383319
// ../shared/index.ts
8331483320
var VISUAL_REGRESSION_CONTEXT = "Visual Regression";
8331583321
var BASE_IMAGES_DIRECTORY = "base-images";
@@ -159664,8 +159670,7 @@ var Jimp = createJimp({
159664159670
plugins: defaultPlugins
159665159671
});
159666159672

159667-
// src/s3-operations.ts
159668-
var s3Client = new S3Client();
159673+
// src/resize.ts
159669159674
async function resizeImageIfNeeded(buffer) {
159670159675
const resizeWidth = getInput("resize-width");
159671159676
const resizeHeight = getInput("resize-height");
@@ -159687,26 +159692,26 @@ async function resizeImageIfNeeded(buffer) {
159687159692
}
159688159693
return image2.getBuffer("image/png");
159689159694
}
159695+
159696+
// src/s3-operations.ts
159690159697
async function checkS3PrefixExists(bucketName, prefix) {
159691159698
try {
159692-
const command = new ListObjectsV2Command({
159699+
const response = await listObjects({
159693159700
Bucket: bucketName,
159694159701
Prefix: prefix,
159695159702
MaxKeys: 1
159696159703
});
159697-
const response = await s3Client.send(command);
159698159704
return (response.Contents?.length ?? 0) > 0;
159699159705
} catch {
159700159706
return false;
159701159707
}
159702159708
}
159703159709
async function downloadS3Directory(bucketName, s3Prefix, localDir) {
159704159710
info(`Downloading base images from s3://${bucketName}/${s3Prefix}`);
159705-
const command = new ListObjectsV2Command({
159711+
const response = await listObjects({
159706159712
Bucket: bucketName,
159707159713
Prefix: s3Prefix
159708159714
});
159709-
const response = await s3Client.send(command);
159710159715
const allObjects = response.Contents ?? [];
159711159716
const baseObjects = allObjects.filter((obj2) => obj2.Key?.endsWith("base.png"));
159712159717
info(`Found ${baseObjects.length} base image(s) to download`);
@@ -159715,11 +159720,10 @@ async function downloadS3Directory(bucketName, s3Prefix, localDir) {
159715159720
const relativePath = Key.substring(s3Prefix.length);
159716159721
const localFilePath = path4.join(localDir, relativePath);
159717159722
await import_fs7.promises.mkdir(path4.dirname(localFilePath), { recursive: true });
159718-
const getCommand = new GetObjectCommand({
159723+
const { Body } = await getObject({
159719159724
Bucket: bucketName,
159720159725
Key
159721159726
});
159722-
const { Body } = await s3Client.send(getCommand);
159723159727
if (Body instanceof import_stream10.Readable) {
159724159728
const writeStream = fs6.createWriteStream(localFilePath);
159725159729
await new Promise((resolve3, reject2) => {
@@ -159748,12 +159752,11 @@ async function uploadLocalDirectoryWithResize(localDir, bucketName, s3Prefix) {
159748159752
const s3Key = path4.join(s3Prefix, file);
159749159753
const fileBuffer = await import_fs7.promises.readFile(localFilePath);
159750159754
const resizedBuffer = await resizeImageIfNeeded(fileBuffer);
159751-
const command = new PutObjectCommand({
159755+
await putObject({
159752159756
Bucket: bucketName,
159753159757
Key: s3Key,
159754159758
Body: resizedBuffer
159755159759
});
159756-
await s3Client.send(command);
159757159760
});
159758159761
info(
159759159762
`Uploaded ${filesFromFailingTests.length} file(s) to s3://${bucketName}/${s3Prefix}`
@@ -159763,12 +159766,11 @@ async function uploadSingleFile(localFilePath, s3Key) {
159763159766
const bucketName = getInput("bucket-name", { required: true });
159764159767
const fileBuffer = await import_fs7.promises.readFile(localFilePath);
159765159768
const resizedBuffer = await resizeImageIfNeeded(fileBuffer);
159766-
const command = new PutObjectCommand({
159769+
await putObject({
159767159770
Bucket: bucketName,
159768159771
Key: s3Key,
159769159772
Body: resizedBuffer
159770159773
});
159771-
await s3Client.send(command);
159772159774
info(`Uploaded ${localFilePath} to s3://${bucketName}/${s3Key}`);
159773159775
}
159774159776
var downloadBaseImages = async () => {
@@ -159836,12 +159838,11 @@ async function uploadOriginalNewPngs(localDir, bucketName, s3Prefix) {
159836159838
const localFilePath = path4.join(localDir, file);
159837159839
const s3Key = path4.join(s3Prefix, file);
159838159840
const fileBuffer = await import_fs7.promises.readFile(localFilePath);
159839-
const command = new PutObjectCommand({
159841+
await putObject({
159840159842
Bucket: bucketName,
159841159843
Key: s3Key,
159842159844
Body: fileBuffer
159843159845
});
159844-
await s3Client.send(command);
159845159846
});
159846159847
info(
159847159848
`Uploaded ${files.length} original new.png file(s) to s3://${bucketName}/${s3Prefix}`

action/dist/main.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

action/src/resize.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { getInput } from '@actions/core';
2+
import { Jimp } from 'jimp';
3+
4+
export async function resizeImageIfNeeded(buffer: Buffer): Promise<Buffer> {
5+
const resizeWidth = getInput('resize-width');
6+
const resizeHeight = getInput('resize-height');
7+
8+
if (!resizeWidth && !resizeHeight) {
9+
return buffer;
10+
}
11+
const width = resizeWidth ? Number(resizeWidth) : undefined;
12+
const height = resizeHeight ? Number(resizeHeight) : undefined;
13+
if ((width && isNaN(width)) || (height && isNaN(height))) {
14+
throw new Error('resize-width and resize-height must be valid numbers');
15+
}
16+
17+
const image = await Jimp.read(buffer);
18+
if (width && height) {
19+
image.cover({ w: width, h: height });
20+
} else if (width) {
21+
image.resize({ w: width });
22+
} else if (height) {
23+
image.resize({ h: height });
24+
}
25+
26+
return image.getBuffer('image/png');
27+
}

action/src/s3-client.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {
2+
S3Client,
3+
ListObjectsV2Command,
4+
ListObjectsV2CommandInput,
5+
ListObjectsV2CommandOutput,
6+
GetObjectCommand,
7+
GetObjectCommandInput,
8+
GetObjectCommandOutput,
9+
PutObjectCommand,
10+
PutObjectCommandInput,
11+
PutObjectCommandOutput
12+
} from '@aws-sdk/client-s3';
13+
14+
const s3Client = new S3Client();
15+
16+
export const listObjects = (
17+
input: ListObjectsV2CommandInput
18+
): Promise<ListObjectsV2CommandOutput> =>
19+
s3Client.send(new ListObjectsV2Command(input));
20+
21+
export const getObject = (
22+
input: GetObjectCommandInput
23+
): Promise<GetObjectCommandOutput> =>
24+
s3Client.send(new GetObjectCommand(input));
25+
26+
export const putObject = (
27+
input: PutObjectCommandInput
28+
): Promise<PutObjectCommandOutput> =>
29+
s3Client.send(new PutObjectCommand(input));

action/src/s3-operations.ts

Lines changed: 8 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import { getInput, info } from '@actions/core';
2-
import {
3-
S3Client,
4-
ListObjectsV2Command,
5-
GetObjectCommand,
6-
PutObjectCommand
7-
} from '@aws-sdk/client-s3';
2+
import { listObjects, getObject, putObject } from './s3-client';
83
import {
94
BASE_IMAGE_NAME,
105
BASE_IMAGES_DIRECTORY,
@@ -18,46 +13,18 @@ import * as fs from 'fs';
1813
import { promises as fsPromises } from 'fs';
1914
import { glob } from 'glob';
2015
import { Readable } from 'stream';
21-
import { Jimp } from 'jimp';
22-
23-
const s3Client = new S3Client();
24-
25-
async function resizeImageIfNeeded(buffer: Buffer): Promise<Buffer> {
26-
const resizeWidth = getInput('resize-width');
27-
const resizeHeight = getInput('resize-height');
28-
29-
if (!resizeWidth && !resizeHeight) {
30-
return buffer;
31-
}
32-
const width = resizeWidth ? Number(resizeWidth) : undefined;
33-
const height = resizeHeight ? Number(resizeHeight) : undefined;
34-
if ((width && isNaN(width)) || (height && isNaN(height))) {
35-
throw new Error('resize-width and resize-height must be valid numbers');
36-
}
37-
38-
const image = await Jimp.read(buffer);
39-
if (width && height) {
40-
image.cover({ w: width, h: height });
41-
} else if (width) {
42-
image.resize({ w: width });
43-
} else if (height) {
44-
image.resize({ h: height });
45-
}
46-
47-
return image.getBuffer('image/png');
48-
}
16+
import { resizeImageIfNeeded } from './resize';
4917

5018
async function checkS3PrefixExists(
5119
bucketName: string,
5220
prefix: string
5321
): Promise<boolean> {
5422
try {
55-
const command = new ListObjectsV2Command({
23+
const response = await listObjects({
5624
Bucket: bucketName,
5725
Prefix: prefix,
5826
MaxKeys: 1
5927
});
60-
const response = await s3Client.send(command);
6128
return (response.Contents?.length ?? 0) > 0;
6229
} catch {
6330
return false;
@@ -71,12 +38,10 @@ async function downloadS3Directory(
7138
): Promise<void> {
7239
info(`Downloading base images from s3://${bucketName}/${s3Prefix}`);
7340

74-
const command = new ListObjectsV2Command({
41+
const response = await listObjects({
7542
Bucket: bucketName,
7643
Prefix: s3Prefix
7744
});
78-
79-
const response = await s3Client.send(command);
8045
const allObjects = response.Contents ?? [];
8146
const baseObjects = allObjects.filter(obj => obj.Key?.endsWith('base.png'));
8247

@@ -90,12 +55,10 @@ async function downloadS3Directory(
9055

9156
await fsPromises.mkdir(path.dirname(localFilePath), { recursive: true });
9257

93-
const getCommand = new GetObjectCommand({
58+
const { Body } = await getObject({
9459
Bucket: bucketName,
9560
Key
9661
});
97-
98-
const { Body } = await s3Client.send(getCommand);
9962
if (Body instanceof Readable) {
10063
const writeStream = fs.createWriteStream(localFilePath);
10164
await new Promise((resolve, reject) => {
@@ -137,13 +100,11 @@ async function uploadLocalDirectoryWithResize(
137100
const fileBuffer = await fsPromises.readFile(localFilePath);
138101
const resizedBuffer = await resizeImageIfNeeded(fileBuffer);
139102

140-
const command = new PutObjectCommand({
103+
await putObject({
141104
Bucket: bucketName,
142105
Key: s3Key,
143106
Body: resizedBuffer
144107
});
145-
146-
await s3Client.send(command);
147108
});
148109

149110
info(
@@ -158,13 +119,11 @@ async function uploadSingleFile(
158119
const bucketName = getInput('bucket-name', { required: true });
159120
const fileBuffer = await fsPromises.readFile(localFilePath);
160121
const resizedBuffer = await resizeImageIfNeeded(fileBuffer);
161-
const command = new PutObjectCommand({
122+
await putObject({
162123
Bucket: bucketName,
163124
Key: s3Key,
164125
Body: resizedBuffer
165126
});
166-
167-
await s3Client.send(command);
168127
info(`Uploaded ${localFilePath} to s3://${bucketName}/${s3Key}`);
169128
}
170129

@@ -248,13 +207,11 @@ async function uploadOriginalNewPngs(
248207

249208
const fileBuffer = await fsPromises.readFile(localFilePath);
250209

251-
const command = new PutObjectCommand({
210+
await putObject({
252211
Bucket: bucketName,
253212
Key: s3Key,
254213
Body: fileBuffer
255214
});
256-
257-
await s3Client.send(command);
258215
});
259216

260217
info(

0 commit comments

Comments
 (0)