Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a README.md file to the backup directory #70

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
);
});
});
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/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,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. 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"
}
Expand Down
Loading