Skip to content

Commit

Permalink
Add README.md to backup base path
Browse files Browse the repository at this point in the history
  • Loading branch information
JackGruber committed Feb 18, 2024
2 parents 0ed6199 + 5e18f15 commit 70104e0
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 9 deletions.
42 changes: 41 additions & 1 deletion __test__/backup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -51,7 +52,10 @@ describe("Backup", function () {
when(spyOnsSettingsValue)
.mockImplementation(() => Promise.resolve("no mockImplementation"))
.calledWith("fileLogLevel").mockImplementation(() => Promise.resolve("error"))
.calledWith("path").mockImplementation(() => Promise.resolve(testPath.backupBasePath));
.calledWith("path").mockImplementation(() => Promise.resolve(testPath.backupBasePath))
.calledWith("zipArchive").mockImplementation(() => "no")
.calledWith("execFinishCmd").mockImplementation(() => "")
.calledWith("usePassword").mockImplementation(() => false);

/* prettier-ignore */
when(spyOnGlobalValue)
Expand All @@ -60,6 +64,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();
Expand Down Expand Up @@ -93,6 +104,7 @@ describe("Backup", function () {
spyOnShowError.mockReset();
spyOnsSettingsValue.mockReset();
spyOnGlobalValue.mockReset();
spyOnDataGet.mockReset();
spyOnSaveBackupInfo.mockReset();
});

Expand Down Expand Up @@ -1014,4 +1026,32 @@ describe("Backup", function () {
expect(backup.log.warn).toHaveBeenCalledTimes(0);
});
});

describe("create backup readme", () => {
it.each([{ backupRetention: 1 }, { backupRetention: 2 }])(
"should create a README.md in the backup directory (case %#)",
async ({ backupRetention }) => {
when(spyOnsSettingsValue)
.calledWith("backupRetention")
.mockImplementation(async () => backupRetention)
.calledWith("backupInfo")
.mockImplementation(() => Promise.resolve("[]"));

backup.backupStartTime = null;
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();
}
);
});
});
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 11 additions & 0 deletions src/Backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ class Backup {

const backupDst = await this.makeBackupSet();

await this.writeReadme(this.backupBasePath);
await joplin.settings.setValue(
"lastBackup",
this.backupStartTime.getTime()
Expand Down Expand Up @@ -684,6 +685,16 @@ class Backup {
}
}

private async writeReadme(backupFolder: string) {
const readmePath = path.join(backupFolder, "README.md");
this.log.info("writeReadme to", readmePath);
const readmeText = i18n.__(
"backupReadme",
this.backupStartTime.toLocaleString()
);
await fs.writeFile(readmePath, readmeText, "utf8");
}

private async backupNotebooks() {
const notebooks = await this.selectNotebooks();

Expand Down
1 change: 1 addition & 0 deletions src/locales/de_DE.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"description": "Befehl/Program nach der Sicherung ausführen"
}
},
"backupReadme": "# Joplin Sicherung\n\nDieser Ordner enthält eine oder mehrere Sicherungen aus der Joplin Note Anwendung.\n\nSiehe [Backup documentation](https://joplinapp.org/plugins/plugin/io.github.jackgruber.backup/#restore) für Informationen wie eine Sicherung wieder hergestellt werden kann.",
"command": {
"createBackup": "Backup erstellen"
}
Expand Down
1 change: 1 addition & 0 deletions src/locales/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
"description": "Execute command when backup is finished"
}
},
"backupReadme": "# Joplin Backup\n\nThis folder contains one or more backups of data from the Joplin note taking application.\n\nSee the [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"
}
Expand Down

0 comments on commit 70104e0

Please sign in to comment.