From 28ce29c74bc97d022774af756a1fea028b3ad0a9 Mon Sep 17 00:00:00 2001
From: Michael Kriese <michael.kriese@visualon.de>
Date: Mon, 6 Apr 2020 12:54:25 +0200
Subject: [PATCH] feat(internal): allow binary artifacts (#5888)

---
 lib/manager/common.ts                        |  3 ++-
 lib/manager/gomod/artifacts.ts               |  7 +++----
 lib/manager/gradle-wrapper/artifacts.spec.ts | 11 +++--------
 lib/util/fs.spec.ts                          | 18 ++++++++++++++++++
 lib/util/fs.ts                               | 17 +++++++++++++----
 lib/util/index.ts                            |  5 ++++-
 6 files changed, 43 insertions(+), 18 deletions(-)
 create mode 100644 lib/util/fs.spec.ts

diff --git a/lib/manager/common.ts b/lib/manager/common.ts
index 2a81ecd286..749bc36624 100644
--- a/lib/manager/common.ts
+++ b/lib/manager/common.ts
@@ -1,6 +1,7 @@
 import { ReleaseType } from 'semver';
 import { RangeStrategy, SkipReason } from '../types';
 import { ValidationMessage, GlobalConfig, UpdateType } from '../config/common';
+import { FileData } from '../platform/common';
 
 export type Result<T> = T | Promise<T>;
 
@@ -189,7 +190,7 @@ export interface ArtifactError {
 
 export interface UpdateArtifactsResult {
   artifactError?: ArtifactError;
-  file?: { name: string; contents: string };
+  file?: FileData;
 }
 
 export interface UpdateArtifact {
diff --git a/lib/manager/gomod/artifacts.ts b/lib/manager/gomod/artifacts.ts
index 24cf7fa0ae..b5db48bfb6 100644
--- a/lib/manager/gomod/artifacts.ts
+++ b/lib/manager/gomod/artifacts.ts
@@ -129,10 +129,9 @@ export async function updateArtifacts({
         });
       }
     }
-    const finalGoModContent = (await readLocalFile(goModFileName)).replace(
-      /\/\/ renovate-replace /g,
-      ''
-    );
+    const finalGoModContent = (
+      await readLocalFile(goModFileName, 'utf8')
+    ).replace(/\/\/ renovate-replace /g, '');
     if (finalGoModContent !== newGoModContent) {
       logger.debug('Found updated go.mod after go.sum update');
       res.push({
diff --git a/lib/manager/gradle-wrapper/artifacts.spec.ts b/lib/manager/gradle-wrapper/artifacts.spec.ts
index 93a89b905c..93d3be3094 100644
--- a/lib/manager/gradle-wrapper/artifacts.spec.ts
+++ b/lib/manager/gradle-wrapper/artifacts.spec.ts
@@ -71,8 +71,7 @@ describe('manager/gradle-wrapper/update', () => {
                   resolve(
                     __dirname,
                     `./__fixtures__/testFiles/${fileProjectPath}`
-                  ),
-                  'utf8'
+                  )
                 ),
               },
             };
@@ -86,14 +85,10 @@ describe('manager/gradle-wrapper/update', () => {
           'gradlew.bat',
         ].forEach(file => {
           expect(
-            readFileSync(
-              resolve(__dirname, `./__fixtures__/testFiles/${file}`),
-              'utf8'
-            )
+            readFileSync(resolve(__dirname, `./__fixtures__/testFiles/${file}`))
           ).toEqual(
             readFileSync(
-              resolve(__dirname, `./__fixtures__/expectedFiles/${file}`),
-              'utf8'
+              resolve(__dirname, `./__fixtures__/expectedFiles/${file}`)
             )
           );
         });
diff --git a/lib/util/fs.spec.ts b/lib/util/fs.spec.ts
new file mode 100644
index 0000000000..10199a460d
--- /dev/null
+++ b/lib/util/fs.spec.ts
@@ -0,0 +1,18 @@
+import { getName } from '../../test/util';
+import { readLocalFile } from './fs';
+
+describe(getName(__filename), () => {
+  describe('readLocalFile', () => {
+    it('reads buffer', async () => {
+      expect(await readLocalFile(__filename)).toBeInstanceOf(Buffer);
+    });
+    it('reads string', async () => {
+      expect(typeof (await readLocalFile(__filename, 'utf8'))).toBe('string');
+    });
+
+    it('does not throw', async () => {
+      // Does not work on FreeBSD: https://nodejs.org/docs/latest-v10.x/api/fs.html#fs_fs_readfile_path_options_callback
+      expect(await readLocalFile(__dirname)).toBeNull();
+    });
+  });
+});
diff --git a/lib/util/fs.ts b/lib/util/fs.ts
index c297595505..3532ac9be7 100644
--- a/lib/util/fs.ts
+++ b/lib/util/fs.ts
@@ -1,10 +1,11 @@
 import { parse, join } from 'upath';
 import { outputFile, readFile } from 'fs-extra';
 import { logger } from '../logger';
+import { RenovateConfig } from '../config/common';
 
 let localDir = '';
 
-export function setFsConfig(config: any): void {
+export function setFsConfig(config: Partial<RenovateConfig>): void {
   localDir = config.localDir;
 }
 
@@ -20,12 +21,20 @@ export function getSiblingFileName(
   return join(subDirectory, otherFileName);
 }
 
-export async function readLocalFile(fileName: string): Promise<string> {
+export async function readLocalFile(fileName: string): Promise<Buffer>;
+export async function readLocalFile(
+  fileName: string,
+  encoding: 'utf8'
+): Promise<string>;
+export async function readLocalFile(
+  fileName: string,
+  encoding?: string
+): Promise<string | Buffer> {
   const localFileName = join(localDir, fileName);
   try {
-    const fileContent = await readFile(localFileName, 'utf8');
+    const fileContent = await readFile(localFileName, encoding);
     return fileContent;
-  } catch (err) /* istanbul ignore next */ {
+  } catch (err) {
     logger.trace({ err }, 'Error reading local file');
     return null;
   }
diff --git a/lib/util/index.ts b/lib/util/index.ts
index ca1081ab0e..5f261f443c 100644
--- a/lib/util/index.ts
+++ b/lib/util/index.ts
@@ -1,7 +1,10 @@
 import { setExecConfig } from './exec';
 import { setFsConfig } from './fs';
+import { RenovateConfig } from '../config/common';
 
-export async function setUtilConfig(config: any): Promise<void> {
+export async function setUtilConfig(
+  config: Partial<RenovateConfig>
+): Promise<void> {
   await setExecConfig(config);
   setFsConfig(config);
 }
-- 
GitLab