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

Get Tests working #32

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"--extensionTestsPath=${workspaceFolder}/out/test/suite/index"
],
"outFiles": ["${workspaceFolder}/out/test/**/*.js"],
"preLaunchTask": "${defaultBuildTask}"
"preLaunchTask": "npm: watch"
}
]
}
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,12 @@ Refer to the [CHANGELOG.md](CHANGELOG.md) file.
You are very welcome to open an issue or a pull request with changes.

If it is your first time with vscode extension, make sure to checkout [Official Guides](https://code.visualstudio.com/api/get-started/your-first-extension).

If you want to run test locally you'll need to yarn install:

```terminal
yarn install
```

Then to run tests use the debugger > `Extention Tests` option

8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"version": "0.6.0",
"publisher": "tchayen",
"engines": {
"vscode": "^1.45.0"
"vscode": "^1.47.2"
},
"categories": [
"Other"
Expand Down Expand Up @@ -68,26 +68,28 @@
"lint": "eslint src --ext ts",
"watch": "tsc -watch -p ./",
"pretest": "yarn run compile && yarn run lint",
"test": "node ./out/test/runTest.js"
"test": "node ./src/test/runTest.js"
},
"devDependencies": {
"@types/glob": "^7.1.1",
"@types/md5": "^2.2.0",
"@types/mocha": "^7.0.2",
"@types/node": "^13.11.0",
"@types/sinon": "^9.0.4",
"@types/vscode": "^1.45.0",
"@typescript-eslint/eslint-plugin": "^2.33.0",
"@typescript-eslint/parser": "^2.33.0",
"eslint": "^6.8.0",
"glob": "^7.1.6",
"mocha": "^7.1.2",
"sinon": "^9.0.2",
"ts-loader": "^7.0.4",
"typescript": "^3.8.3",
"vscode-test": "^1.3.0",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11"
},
"dependencies": {
"@types/md5": "^2.2.0",
"md5": "^2.2.1",
"remark-frontmatter": "^2.0.0",
"remark-parse": "^8.0.2",
Expand Down
10 changes: 6 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import * as vscode from "vscode";
import { TextDecoder } from "util";
import * as path from "path";
import { parseFile, parseDirectory, learnFileId } from "./parsing";
import { filterNonExistingEdges, getColumnSetting, getConfiguration, getFileTypesSetting } from "./utils";
import { parseDirectory, learnFileId, processFile } from "./parsing";
import { filterNonExistingEdges, getColumnSetting, getConfiguration, getFileTypesSetting, getDot } from "./utils";

import { Graph } from "./types";

const watch = (
Expand Down Expand Up @@ -30,7 +31,7 @@ const watch = (

// Watch file changes in case user adds a link.
watcher.onDidChange(async (event) => {
await parseFile(graph, event.path);
processFile(graph, event.path);
filterNonExistingEdges(graph);
sendGraph();
});
Expand Down Expand Up @@ -129,7 +130,7 @@ export function activate(context: vscode.ExtensionContext) {
};

await parseDirectory(graph, vscode.workspace.rootPath, learnFileId);
await parseDirectory(graph, vscode.workspace.rootPath, parseFile);
await parseDirectory(graph, vscode.workspace.rootPath, processFile);
filterNonExistingEdges(graph);

const d3Uri = panel.webview.asWebviewUri(
Expand All @@ -138,6 +139,7 @@ export function activate(context: vscode.ExtensionContext) {

panel.webview.html = await getWebviewContent(context, graph, d3Uri);

console.log(getDot(graph));
watch(context, panel, graph);
})
);
Expand Down
57 changes: 52 additions & 5 deletions src/parsing.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import * as vscode from "vscode";
import * as path from "path";
import * as fs from "fs";
import * as util from "util";
import * as unified from "unified";
import * as markdown from "remark-parse";
import * as wikiLinkPlugin from "remark-wiki-link";
import * as frontmatter from "remark-frontmatter";
import { MarkdownNode, Graph } from "./types";
import { TextDecoder } from "util";
import { findTitle, findLinks, id, FILE_ID_REGEXP, getFileTypesSetting, getConfiguration } from "./utils";
import { findTitle, findLinks, id, getFileIdRegexp, getFileTypesSetting } from "./utils";
import { basename } from "path";

let idToPath: Record<string, string> = {};

const readFileAsync = util.promisify(fs.readFile);

/**
* ??
* @param id ??
*/
export const idResolver = (id: string) => {
const filePath = idToPath[id];
if (filePath === undefined) {
Expand All @@ -25,9 +33,32 @@ const parser = unified()
.use(wikiLinkPlugin, { pageResolver: idResolver })
.use(frontmatter);

export const parseFile = async (graph: Graph, filePath: string) => {
const buffer = await vscode.workspace.fs.readFile(vscode.Uri.file(filePath));
const content = new TextDecoder("utf-8").decode(buffer);
/**
* Wrapper for `parseFile` that reads a file. Uses `vscode.workspace.fs`.
* @param graph object that will be altered.
* @param filePath absolute path to the file. Used for reading the file.
*/
export const processFile = async (graph: Graph, filePath: string) => {
let content = await readFile(filePath);
return parseFile(graph, filePath, content);
};

/**
* Read file, return text.
* @param filePath absolute path to the file. Used for reading the file.
*/
export const readFile = async (filePath: string) => {
return await readFileAsync(filePath, {encoding:'utf8'});
};

/**
* Alters given graph, adding a node and some edges if file is properly
* structured.
* @param graph object that will be altered.
* @param filePath absolute path to the file. Isn't used for reading the file.
* @param content content of the file as utf-8 string.
*/
export const parseFile = (graph: Graph, filePath: string, content: string) => {
const ast: MarkdownNode = parser.parse(content);

let title: string | null = findTitle(ast);
Expand Down Expand Up @@ -64,14 +95,23 @@ export const parseFile = async (graph: Graph, filePath: string) => {
}
};

/**
* For given file path, returns its ID or null. Uses `vscode.workspace.fs`.
* @param filePath absolute path of the file.
*/
export const findFileId = async (filePath: string): Promise<string | null> => {
const buffer = await vscode.workspace.fs.readFile(vscode.Uri.file(filePath));
const content = new TextDecoder("utf-8").decode(buffer);

const match = content.match(FILE_ID_REGEXP);
const match = content.match(getFileIdRegexp());
return match ? match[1] : null;
};

/**
* Populates `idToPath` with ID from the file if one is found there.
* @param _graph unused.
* @param filePath absolute path of the file.
*/
export const learnFileId = async (_graph: Graph, filePath: string) => {
const id = await findFileId(filePath);
if (id !== null) {
Expand All @@ -85,6 +125,13 @@ export const learnFileId = async (_graph: Graph, filePath: string) => {
idToPath[fileNameWithoutExt] = filePath;
};

/**
* Recursively reads content of the given directory and calls specified
* callback on each file (not a symlink) with `*.md` extensions.
* @param graph object to be altered.
* @param directory path of the directory.
* @param fileCallback
*/
export const parseDirectory = async (
graph: Graph,
directory: string,
Expand Down
1 change: 1 addition & 0 deletions src/test/data/1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Test
3 changes: 3 additions & 0 deletions src/test/data/2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Test 2

This links to [1](1.md)
Loading