Forráskód Böngészése

Added tests for `writeFile` method of IdentifierNamesCacheFileUtils

sanex 4 éve
szülő
commit
13a7302853

A különbségek nem kerülnek megjelenítésre, a fájl túl nagy
+ 0 - 0
dist/index.cli.js


A különbségek nem kerülnek megjelenítésre, a fájl túl nagy
+ 0 - 0
dist/index.js


+ 1 - 4
src/cli/utils/IdentifierNamesCacheFileUtils.ts

@@ -70,10 +70,7 @@ export class IdentifierNamesCacheFileUtils {
 
         if (!fileData.content) {
             // Initial state of identifier names cache file
-            return {
-                globalIdentifiers: {},
-                propertyIdentifiers: {}
-            };
+            return {};
         }
 
         try {

+ 62 - 1
test/unit-tests/cli/utils/IdentifierNamesCacheFileUtils.spec.ts

@@ -19,7 +19,7 @@ describe('IdentifierNamesCacheFileUtils', () => {
         propertyIdentifiers: {
             bar: '_0x654321'
         }
-    };
+    }
     const fileContent: string = JSON.stringify(expectedIdentifierNamesCache);
     const tmpDirectoryPath: string = path.join('test', 'tmp');
 
@@ -144,6 +144,67 @@ describe('IdentifierNamesCacheFileUtils', () => {
         });
     });
 
+    describe('writeFile', () => {
+        describe('Variant #1: Should write identifier names cache back to the file', () => {
+            describe('Variant #1: identifier names cache file does not exist', () => {
+                const expectedUpdatedIdentifierNamesCache: TIdentifierNamesCache = {
+                    ...expectedIdentifierNamesCache,
+                    globalIdentifiers: {
+                        ...expectedIdentifierNamesCache.globalIdentifiers,
+                        baz: '_0x987654'
+                    }
+                };
+
+                const tmpFileName: string = 'cache.json';
+                const inputPath: string = path.join(tmpDirectoryPath, tmpFileName);
+
+                let updatedIdentifierNamesCache: TIdentifierNamesCache | null;
+
+                before(() => {
+                    new IdentifierNamesCacheFileUtils(inputPath).writeFile(expectedUpdatedIdentifierNamesCache);
+                    updatedIdentifierNamesCache = JSON.parse(fs.readFileSync(inputPath, 'utf8'));
+                });
+
+                it('should correctly write updated identifier names to the cache file', () => {
+                    assert.deepEqual(updatedIdentifierNamesCache, expectedUpdatedIdentifierNamesCache);
+                });
+
+                after(() => {
+                    fs.unlinkSync(inputPath);
+                });
+            });
+
+            describe('Variant #2 identifier names cache file is exist', () => {
+                const expectedUpdatedIdentifierNamesCache: TIdentifierNamesCache = {
+                    ...expectedIdentifierNamesCache,
+                    globalIdentifiers: {
+                        ...expectedIdentifierNamesCache.globalIdentifiers,
+                        baz: '_0x987654'
+                    }
+                };
+
+                const tmpFileName: string = 'cache.json';
+                const inputPath: string = path.join(tmpDirectoryPath, tmpFileName);
+
+                let updatedIdentifierNamesCache: TIdentifierNamesCache | null;
+
+                before(() => {
+                    fs.writeFileSync(inputPath, fileContent);
+                    new IdentifierNamesCacheFileUtils(inputPath).writeFile(expectedUpdatedIdentifierNamesCache);
+                    updatedIdentifierNamesCache = JSON.parse(fs.readFileSync(inputPath, 'utf8'));
+                });
+
+                it('should correctly write updated identifier names to the cache file', () => {
+                    assert.deepEqual(updatedIdentifierNamesCache, expectedUpdatedIdentifierNamesCache);
+                });
+
+                after(() => {
+                    fs.unlinkSync(inputPath);
+                });
+            });
+        });
+    });
+
     after(() => {
         rimraf.sync(tmpDirectoryPath);
     });

Nem az összes módosított fájl került megjelenítésre, mert túl sok fájl változott