Skip to content

Commit

Permalink
Copy permissions when copying files (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
cbush authored Jul 27, 2021
1 parent a95de38 commit ca65432
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/cli/commands/copy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe("copy", () => {
beforeEach(getBluehawk.reset);
beforeEach(System.useMemfs);

it("copies", async (done) => {
it("copies", async () => {
const rootPath = "/path/to/project";
const destinationPath = "/destination";
await System.fs.mkdir(rootPath, {
Expand All @@ -26,7 +26,6 @@ describe("copy", () => {
expect(sourceList).toStrictEqual(["test.txt"]);
const destinationList = await System.fs.readdir(destinationPath);
expect(destinationList).toStrictEqual(sourceList);
done();
});

it("copies binary files", async () => {
Expand Down Expand Up @@ -56,4 +55,36 @@ describe("copy", () => {
expect(destinationList).toStrictEqual(sourceList);
expect(didCallBinaryFileForPath).toBe(filePath);
});

it("copies permissions", async () => {
const rootPath = "/path/to/project";
const destinationPath = "/destination";
await System.fs.mkdir(rootPath, {
recursive: true,
});
await System.fs.mkdir(destinationPath, {
recursive: true,
});
const binPath = Path.join(rootPath, "test.bin");
await System.fs.writeFile(binPath, new Uint8Array([0, 1, 2, 3, 4, 5]));
await System.fs.chmod(binPath, 0o100755);
const textPath = Path.join(rootPath, "test.sh");
await System.fs.writeFile(textPath, "# this is a script", "utf8");
await System.fs.chmod(textPath, 0o100755);
const errors = await copy({
destination: destinationPath,
rootPath,
});

expect(errors).toStrictEqual([]);
const sourceList = await System.fs.readdir(rootPath);
expect(sourceList).toStrictEqual(["test.bin", "test.sh"]);
const destinationList = await System.fs.readdir(destinationPath);
expect(destinationList).toStrictEqual(sourceList);
const modes = [
await System.fs.stat(Path.join(destinationPath, "test.bin")),
await System.fs.stat(Path.join(destinationPath, "test.sh")),
].map(({ mode }) => mode);
expect(modes).toStrictEqual([0o100755, 0o100755]);
});
});
20 changes: 20 additions & 0 deletions src/cli/commands/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export const copy = async (args: CopyArgs): Promise<string[]> => {
try {
await System.fs.mkdir(directory, { recursive: true });
await System.fs.copyFile(filePath, targetPath);
await copyPermissions({ to: targetPath, from: filePath });
} catch (error) {
const message = `Failed to copy file ${filePath} to ${targetPath}: ${error.message}`;
console.error(message);
Expand Down Expand Up @@ -108,6 +109,10 @@ export const copy = async (args: CopyArgs): Promise<string[]> => {
try {
await System.fs.mkdir(directory, { recursive: true });
await System.fs.writeFile(targetPath, document.text.toString(), "utf8");
await copyPermissions({
to: targetPath,
from: document.path,
});
} catch (error) {
const message = `Failed to write file ${targetPath} (based on ${parseResult.source.path}): ${error.message}`;
console.error(message);
Expand Down Expand Up @@ -157,3 +162,18 @@ const commandModule: CommandModule<MainArgs & { rootPath: string }, CopyArgs> =
};

export default commandModule;

/**
Copy permissions (using stat/chmod) to the given path from the file at the
given path.
*/
export const copyPermissions = async ({
to,
from,
}: {
to: string;
from: string;
}): Promise<void> => {
const { mode } = await System.fs.stat(from);
await System.fs.chmod(to, mode);
};

0 comments on commit ca65432

Please sign in to comment.