Skip to content
Open
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
25 changes: 20 additions & 5 deletions src/utils/helpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fsOperation from "fileSystem";
import alert from "dialogs/alert";
import escapeStringRegexp from "escape-string-regexp";
import adRewards from "lib/adRewards";
import config from "lib/config";
import fileIcons from "lib/fileIcons";
Expand Down Expand Up @@ -286,17 +285,33 @@ export default {
updateUriOfAllActiveFiles(oldUrl, newUrl) {
const files = editorManager.files;
const { url } = Url.parse(oldUrl);
const base = url.endsWith("/") ? url.slice(0, -1) : url;

// A SAF storage root is a bare tree URI; its documents are `<tree>::<docId>`.
// Anywhere else `::` is just part of a file or folder name.
let isSafTreeRoot = false;
try {
isSafTreeRoot = Uri.parse(base).rootUri === base;
} catch (_) {
// not a SAF content URI
}

for (let file of files) {
if (!file.uri) continue;
const fileUrl = Url.parse(file.uri).url;
if (new RegExp("^" + escapeStringRegexp(url)).test(fileUrl)) {
if (newUrl) {
file.uri = Url.join(newUrl, file.filename);
} else {
if (!newUrl) {
if (
Url.isSameOrDescendant(fileUrl, base) ||
(isSafTreeRoot && fileUrl.startsWith(`${base}::`))
) {
file.uri = null;
}
continue;
}
if (!Url.isSameOrDescendant(fileUrl, base)) continue;
// Keep the file's path relative to the renamed/moved folder.
const relative = fileUrl.slice(base.length + 1);
file.uri = relative ? Url.join(newUrl, relative) : newUrl;
}

editorManager.onupdate("file-delete");
Expand Down
125 changes: 125 additions & 0 deletions tests/unit/updateUriOfAllActiveFiles.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { afterEach, expect, test, vi } from "vitest";

vi.mock("fileSystem", () => ({ default: vi.fn() }));
vi.mock("dialogs/alert", () => ({ default: vi.fn() }));
vi.mock("lib/adRewards", () => ({ default: {} }));
vi.mock("lib/config", () => ({ default: {} }));
vi.mock("lib/fileIcons", () => ({ default: {} }));
vi.mock("lib/startAd", () => ({
interstitialAd: {},
requestBannerForPage: vi.fn(),
}));

import helpers from "utils/helpers";

afterEach(() => vi.unstubAllGlobals());

function openTabs(...uris) {
const files = uris.map((uri) => ({
uri,
filename: uri.slice(uri.lastIndexOf("/") + 1),
}));
vi.stubGlobal("editorManager", {
files,
onupdate: vi.fn(),
emit: vi.fn(),
});
return files;
}

const uris = (files) => files.map((file) => file.uri);

test("renaming a folder keeps the sub-path of nested open files", () => {
const files = openTabs(
"file:///sdcard/proj/src/index.js",
"file:///sdcard/proj/src/components/Button.js",
"file:///sdcard/proj/src/a/b/c.js",
);
helpers.updateUriOfAllActiveFiles(
"file:///sdcard/proj/src/",
"file:///sdcard/proj/lib/",
);
expect(uris(files)).toEqual([
"file:///sdcard/proj/lib/index.js",
"file:///sdcard/proj/lib/components/Button.js",
"file:///sdcard/proj/lib/a/b/c.js",
]);
});

test("renaming a folder leaves sibling folders that share its name prefix alone", () => {
const files = openTabs(
"file:///sdcard/proj/src/index.js",
"file:///sdcard/proj/src-old/legacy.js",
"file:///sdcard/proj/src2/main.js",
);
helpers.updateUriOfAllActiveFiles(
"file:///sdcard/proj/src",
"file:///sdcard/proj/lib",
);
expect(uris(files)).toEqual([
"file:///sdcard/proj/lib/index.js",
"file:///sdcard/proj/src-old/legacy.js",
"file:///sdcard/proj/src2/main.js",
]);
});

test("deleting a folder only detaches files inside it", () => {
const files = openTabs(
"file:///sdcard/proj/src/components/Button.js",
"file:///sdcard/proj/src-old/legacy.js",
);
helpers.updateUriOfAllActiveFiles("file:///sdcard/proj/src", null);
expect(uris(files)).toEqual([null, "file:///sdcard/proj/src-old/legacy.js"]);
});

test("renaming an open file points its tab at the new name", () => {
const files = openTabs(
"file:///sdcard/proj/old.js",
"file:///sdcard/proj/old.json",
);
helpers.updateUriOfAllActiveFiles(
"file:///sdcard/proj/old.js",
"file:///sdcard/proj/new.js",
);
expect(uris(files)).toEqual([
"file:///sdcard/proj/new.js",
"file:///sdcard/proj/old.json",
]);
});

test("removing a SAF storage detaches the tabs opened from it", () => {
const tree =
"content://com.android.externalstorage.documents/tree/primary%3ATesthtml";
const files = openTabs(
`${tree}::primary:Testhtml/Styles/index.html`,
`${tree}2::primary:Testhtml2/index.html`,
);
helpers.updateUriOfAllActiveFiles(tree, null);
expect(uris(files)).toEqual([null, `${tree}2::primary:Testhtml2/index.html`]);
});

test("deleting a non-SAF folder leaves a sibling whose name continues with :: attached", () => {
const files = openTabs(
"sftp://profile/project/src/index.js",
"sftp://profile/project/src::backup/index.js",
);
helpers.updateUriOfAllActiveFiles("sftp://profile/project/src", null);
expect(uris(files)).toEqual([
null,
"sftp://profile/project/src::backup/index.js",
]);
});

test("deleting a folder inside a SAF tree leaves a sibling whose name continues with :: attached", () => {
const tree =
"content://com.android.externalstorage.documents/tree/primary%3ATesthtml";
const files = openTabs(
`${tree}::primary:Testhtml/src/index.js`,
`${tree}::primary:Testhtml/src::backup/index.js`,
);
helpers.updateUriOfAllActiveFiles(`${tree}::primary:Testhtml/src`, null);
expect(uris(files)).toEqual([
null,
`${tree}::primary:Testhtml/src::backup/index.js`,
]);
});