From 7466f6ef3a5ad19bc4aa7127adb51fb3d90c3c60 Mon Sep 17 00:00:00 2001 From: kobenguyent Date: Tue, 4 Jun 2024 11:19:56 +0200 Subject: [PATCH 1/4] feat: provide an opt to skip s3 upload --- README.md | 8 +++++--- src/index.ts | 22 +++++++++++++--------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 333cc20..cb42a87 100644 --- a/README.md +++ b/README.md @@ -161,10 +161,10 @@ Set `output` to where the generated report is to be stored. Default is the outpu ### AWS Support AWS S3 support to upload and download various images is also provided. It can be used by adding the *aws* code inside `"ResembleHelper"` in the `"helpers"` section in config file. The final result should look like: -```json +```js { "helpers": { - "ResembleHelper" : { + "ResembleHelper": { "require": "codeceptjs-resemblehelper", "baseFolder": "", "diffFolder": "", @@ -172,7 +172,8 @@ It can be used by adding the *aws* code inside `"ResembleHelper"` in the `"helpe "accessKeyId" : "", "secretAccessKey": "", "region": "", - "bucketName": "" + "bucketName": "", + "skipS3Upload": true, } } } @@ -183,6 +184,7 @@ This base image has to be located inside a folder named "*base*". The resultant output image will be uploaded in a folder named "*output*" and diff image will be uploaded to a folder named "*diff*" in the S3 bucket. If the `prepareBaseImage` option is marked `true`, then the generated base image will be uploaded to a folder named "*base*" in the S3 bucket. > Note: The tests may take a bit longer to run when the AWS configuration is provided as determined by the internet speed to upload/download images. +> Note: if you want to skip the s3 upload, set skipS3Upload to true. ### Other S3 Providers The same configuration as above, but with *endpoint* field: diff --git a/src/index.ts b/src/index.ts index 99120fd..47bb1ca 100644 --- a/src/index.ts +++ b/src/index.ts @@ -441,15 +441,19 @@ class ResembleHelper extends Helper { await this._addAttachment(baseImage, misMatch, options); await this._addMochaContext(baseImage, misMatch, options); if (awsC !== undefined) { - await this._upload( - awsC.accessKeyId, - awsC.secretAccessKey, - awsC.region, - awsC.bucketName, - baseImage, - options, - awsC.endpoint, - ); + if (awsC.skipS3Upload) { + this.debug(`Uploading to S3 is skipped due to skipS3Upload is set`); + } else { + await this._upload( + awsC.accessKeyId, + awsC.secretAccessKey, + awsC.region, + awsC.bucketName, + baseImage, + options, + awsC.endpoint, + ); + } } this.debug(`MisMatch Percentage Calculated is ${misMatch} for baseline ${baseImage}`); From 58b16c8a9849c6af02d661762a91b027ec4c1e2e Mon Sep 17 00:00:00 2001 From: kobenguyent Date: Tue, 4 Jun 2024 11:22:11 +0200 Subject: [PATCH 2/4] lint fix --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 47bb1ca..a22324f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -442,7 +442,7 @@ class ResembleHelper extends Helper { await this._addMochaContext(baseImage, misMatch, options); if (awsC !== undefined) { if (awsC.skipS3Upload) { - this.debug(`Uploading to S3 is skipped due to skipS3Upload is set`); + this.debug("Uploading to S3 is skipped due to skipS3Upload is set"); } else { await this._upload( awsC.accessKeyId, From e213acecdaa1dae5d3f93b4e9918745187fdab68 Mon Sep 17 00:00:00 2001 From: kobenguyent Date: Tue, 11 Jun 2024 15:37:58 +0200 Subject: [PATCH 3/4] fix: logic --- README.md | 4 ++-- src/index.ts | 58 ++++++++++++++++++++++++++++------------------------ 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index cb42a87..1f33fd7 100644 --- a/README.md +++ b/README.md @@ -173,7 +173,7 @@ It can be used by adding the *aws* code inside `"ResembleHelper"` in the `"helpe "secretAccessKey": "", "region": "", "bucketName": "", - "skipS3Upload": true, + "uploadOnlyBaseImage": true, } } } @@ -184,7 +184,7 @@ This base image has to be located inside a folder named "*base*". The resultant output image will be uploaded in a folder named "*output*" and diff image will be uploaded to a folder named "*diff*" in the S3 bucket. If the `prepareBaseImage` option is marked `true`, then the generated base image will be uploaded to a folder named "*base*" in the S3 bucket. > Note: The tests may take a bit longer to run when the AWS configuration is provided as determined by the internet speed to upload/download images. -> Note: if you want to skip the s3 upload, set skipS3Upload to true. +> Note: if you want to skip the s3 upload for other folders like diff or output, set `uploadOnlyBaseImage` to true, then only base folder is uploaded to S3. ### Other S3 Providers The same configuration as above, but with *endpoint* field: diff --git a/src/index.ts b/src/index.ts index a22324f..316a7fc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -281,6 +281,7 @@ class ResembleHelper extends Helper { baseImage: any, options: any, endpoint: Endpoint, + uploadOnlyBaseImage: boolean, ) { console.log("Starting Upload... "); const s3 = new AWS.S3({ @@ -289,6 +290,35 @@ class ResembleHelper extends Helper { region: region, endpoint, }); + + // If prepareBaseImage is false, then it won't upload the baseImage. However, this parameter is not considered if the config file has a prepareBaseImage set to true. + if (this._getPrepareBaseImage(options)) { + const baseImageName = this._getBaseImageName(baseImage, options); + + fs.readFile(this._getBaseImagePath(baseImage, options), (err: any, data: any) => { + if (err) throw err; + else { + const base64data = new Buffer(data, "binary"); + const params = { + Bucket: bucketName, + Key: `base/${baseImageName}`, + Body: base64data, + }; + s3.upload(params, (uErr: any, uData: { Location: any }) => { + if (uErr) throw uErr; + console.log(`Base Image uploaded at ${uData.Location}`); + }); + } + }); + } else { + console.log("Not Uploading base Image"); + } + + if (uploadOnlyBaseImage) { + this.debug("Only the Base Image is uploaded to S3. Skipping upload of diff and output folders!"); + return; + } + fs.readFile(this._getActualImagePath(baseImage), (err: any, data: any) => { if (err) throw err; const base64data = new Buffer(data, "binary"); @@ -317,29 +347,6 @@ class ResembleHelper extends Helper { }); } }); - - // If prepareBaseImage is false, then it won't upload the baseImage. However, this parameter is not considered if the config file has a prepareBaseImage set to true. - if (this._getPrepareBaseImage(options)) { - const baseImageName = this._getBaseImageName(baseImage, options); - - fs.readFile(this._getBaseImagePath(baseImage, options), (err: any, data: any) => { - if (err) throw err; - else { - const base64data = new Buffer(data, "binary"); - const params = { - Bucket: bucketName, - Key: `base/${baseImageName}`, - Body: base64data, - }; - s3.upload(params, (uErr: any, uData: { Location: any }) => { - if (uErr) throw uErr; - console.log(`Base Image uploaded at ${uData.Location}`); - }); - } - }); - } else { - console.log("Not Uploading base Image"); - } } /** @@ -441,9 +448,6 @@ class ResembleHelper extends Helper { await this._addAttachment(baseImage, misMatch, options); await this._addMochaContext(baseImage, misMatch, options); if (awsC !== undefined) { - if (awsC.skipS3Upload) { - this.debug("Uploading to S3 is skipped due to skipS3Upload is set"); - } else { await this._upload( awsC.accessKeyId, awsC.secretAccessKey, @@ -452,8 +456,8 @@ class ResembleHelper extends Helper { baseImage, options, awsC.endpoint, + awsC.uploadOnlyBaseImage ); - } } this.debug(`MisMatch Percentage Calculated is ${misMatch} for baseline ${baseImage}`); From 3ebd1464c19bf1e1098fd4c467c3000c2f393a37 Mon Sep 17 00:00:00 2001 From: kobenguyent Date: Tue, 11 Jun 2024 15:42:35 +0200 Subject: [PATCH 4/4] fix: lint issue --- src/index.ts | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/index.ts b/src/index.ts index 316a7fc..675d4fa 100644 --- a/src/index.ts +++ b/src/index.ts @@ -448,16 +448,16 @@ class ResembleHelper extends Helper { await this._addAttachment(baseImage, misMatch, options); await this._addMochaContext(baseImage, misMatch, options); if (awsC !== undefined) { - await this._upload( - awsC.accessKeyId, - awsC.secretAccessKey, - awsC.region, - awsC.bucketName, - baseImage, - options, - awsC.endpoint, - awsC.uploadOnlyBaseImage - ); + await this._upload( + awsC.accessKeyId, + awsC.secretAccessKey, + awsC.region, + awsC.bucketName, + baseImage, + options, + awsC.endpoint, + awsC.uploadOnlyBaseImage, + ); } this.debug(`MisMatch Percentage Calculated is ${misMatch} for baseline ${baseImage}`);