From b77920310c9a2cfdc772872e3cab0f9b7095ef0b Mon Sep 17 00:00:00 2001 From: Henry H Date: Wed, 14 Feb 2024 16:21:25 -0800 Subject: [PATCH] Add automated test --- __test__/backup.test.ts | 37 +++++++++++++++++++++++++++++++++++++ package.json | 2 +- src/locales/en_US.json | 2 +- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/__test__/backup.test.ts b/__test__/backup.test.ts index c557dde..49e6832 100644 --- a/__test__/backup.test.ts +++ b/__test__/backup.test.ts @@ -27,6 +27,7 @@ let spyOnLogWarn = null; let spyOnLogError = null; let spyOnShowError = null; let spyOnSaveBackupInfo = null; +let spyOnDataGet = null; const spyOnsSettingsValue = jest.spyOn(joplin.settings, "value"); const spyOnGlobalValue = jest.spyOn(joplin.settings, "globalValue"); @@ -60,6 +61,13 @@ describe("Backup", function () { .calledWith("locale").mockImplementation(() => Promise.resolve("en_US")) .calledWith("templateDir").mockImplementation(() => Promise.resolve(testPath.templates)); + spyOnDataGet = jest + .spyOn(joplin.data, "get") + .mockImplementation(async (_path, _query) => ({ + items: [], + hasMore: false, + })); + await createTestStructure(); backup = new Backup() as any; backup.backupStartTime = new Date(); @@ -93,6 +101,7 @@ describe("Backup", function () { spyOnShowError.mockReset(); spyOnsSettingsValue.mockReset(); spyOnGlobalValue.mockReset(); + spyOnDataGet.mockReset(); spyOnSaveBackupInfo.mockReset(); }); @@ -1014,4 +1023,32 @@ describe("Backup", function () { expect(backup.log.warn).toHaveBeenCalledTimes(0); }); }); + + describe("create backup readme", () => { + it("should create a README.md in the backup directory", async () => { + backup.backupStartTime = null; + backup.passwordEnabled = false; + + when(spyOnsSettingsValue) + .calledWith("zipArchive") + .mockImplementation(() => "no"); + when(spyOnsSettingsValue) + .calledWith("execFinishCmd") + .mockImplementation(() => ""); + + await backup.start(); + + // Should exist and be non-empty + const readmePath = path.join( + testPath.backupBasePath, + "JoplinBackup", + "README.md" + ); + expect(await fs.pathExists(readmePath)).toBe(true); + expect(await fs.readFile(readmePath, "utf8")).not.toBe(""); + + // Prevent "open handle" errors + backup.stopTimer(); + }); + }); }); diff --git a/package.json b/package.json index b937f80..52d6de8 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "husky": "^6.0.0", "jest": "^27.0.4", "jest-when": "^3.3.1", - "joplinplugindevtools": "^1.0.15", + "joplinplugindevtools": "^1.0.16", "lint-staged": "^11.0.0", "mime": "^2.5.2", "on-build-webpack": "^0.1.0", diff --git a/src/locales/en_US.json b/src/locales/en_US.json index 771d14c..f7db718 100644 --- a/src/locales/en_US.json +++ b/src/locales/en_US.json @@ -86,7 +86,7 @@ "description": "Execute command when backup is finished" } }, - "backupReadme": "# Joplin Backup\n\nThe backups in this folder were created with the Simple Backup plugin for Joplin. The most recent backup was created on %s.\n\nSee the [Simple Backup documentation](https://joplinapp.org/plugins/plugin/io.github.jackgruber.backup/#restore) for information about how to restore from this backup.", + "backupReadme": "# Joplin Backup\n\nThis folder contains one or more backups of data from the Joplin note taking application. The most recent backup was created on %s.\n\nSee the [Simple Backup documentation](https://joplinapp.org/plugins/plugin/io.github.jackgruber.backup/#restore) for information about how to restore from this backup.", "command": { "createBackup": "Create backup" }