Skip to content

Commit f83bc25

Browse files
committed
feat: Add mock interpreter for FileSystem effects
1 parent 104c4e8 commit f83bc25

2 files changed

Lines changed: 83 additions & 3 deletions

File tree

sources/fileSystem.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { taggedSum } from "daggy";
44
import fs from "fs";
55
import path from "path";
6+
import { assoc } from "ramda";
67

78
import { interpreter, send } from "./eff";
89

@@ -37,3 +38,31 @@ export const interpretLocalFileSystem = (fileSystemRoot: string) =>
3738
),
3839
}),
3940
});
41+
42+
export const interpretMockFileSystem = ({
43+
fileSystemRoot,
44+
startingFileSystem,
45+
onUpdate,
46+
}: {
47+
fileSystemRoot: string,
48+
startingFileSystem: { ... },
49+
onUpdate?: ({ ... }) => void,
50+
}) => {
51+
let fileSystem = startingFileSystem;
52+
53+
return interpreter({
54+
predicate: x => FileSystem.is(x),
55+
handler: fileSystemEffect =>
56+
fileSystemEffect.cata({
57+
readFile: filePath => continuation =>
58+
continuation(fileSystem[path.resolve(fileSystemRoot, filePath)]),
59+
writeFile: (filePath, content) => continuation => {
60+
fileSystem = assoc(path.resolve(fileSystemRoot, filePath))(content)(
61+
fileSystem,
62+
);
63+
if (onUpdate) onUpdate({ ...fileSystem });
64+
return continuation(null);
65+
},
66+
}),
67+
});
68+
};

sources/fileSystem.test.js

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@ import fs from "fs";
55
import path from "path";
66

77
import { run } from "./eff";
8-
import { readFile, writeFile, interpretLocalFileSystem } from "./fileSystem";
8+
import {
9+
readFile,
10+
writeFile,
11+
interpretLocalFileSystem,
12+
interpretMockFileSystem,
13+
} from "./fileSystem";
914
import { tempDirectory } from "./testUtils.js";
1015

11-
test.cb("Read file", t => {
16+
test.cb("Local File System – Read file", t => {
1217
const a = "Hello, World! I'm reading!";
1318
const fileName = "./helloWorldReading.txt";
1419

@@ -28,7 +33,7 @@ test.cb("Read file", t => {
2833
});
2934
});
3035

31-
test.cb("Write file", t => {
36+
test.cb("Local File System – Write file", t => {
3237
const a = "Hello, World! I'm writing!";
3338
const fileName = "./helloWorldWriting.txt";
3439

@@ -47,3 +52,49 @@ test.cb("Write file", t => {
4752
})(application);
4853
});
4954
});
55+
56+
test.cb("Mock File System – Read file", t => {
57+
const a = "Hello, World! I'm writing!";
58+
const fileName = "./helloWorldWriting.txt";
59+
const directory = "/directory";
60+
61+
const application = readFile(fileName);
62+
63+
let fileSystem = { "/directory/helloWorldWriting.txt": a };
64+
65+
run(
66+
interpretMockFileSystem({
67+
fileSystemRoot: directory,
68+
startingFileSystem: fileSystem,
69+
onUpdate: newFileSystem => {
70+
fileSystem = newFileSystem;
71+
},
72+
}),
73+
)(b => {
74+
t.is(b, a);
75+
t.end();
76+
})(application);
77+
});
78+
79+
test.cb("Mock File System – Write file", t => {
80+
const a = "Hello, World! I'm writing!";
81+
const fileName = "./helloWorldWriting.txt";
82+
const directory = "/directory";
83+
84+
const application = writeFile(fileName, a);
85+
86+
let fileSystem: { [string]: string } = {};
87+
88+
run(
89+
interpretMockFileSystem({
90+
fileSystemRoot: directory,
91+
startingFileSystem: fileSystem,
92+
onUpdate: newFileSystem => {
93+
fileSystem = newFileSystem;
94+
},
95+
}),
96+
)(() => {
97+
t.is(fileSystem["/directory/helloWorldWriting.txt"], a);
98+
t.end();
99+
})(application);
100+
});

0 commit comments

Comments
 (0)