blob: 5200af72073d845ecb1d009bba89bec3bf6489b0 [file] [log] [blame] [edit]
// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import * as assert from 'assert';
import * as vscode from 'vscode';
import * as path from 'path';
import { ToolFinder } from '../../tool_finder';
import { anEventOf, noTimeout, separateTimeoutBudget } from './utils';
import { describe, it, before, beforeEach, after } from 'mocha';
import * as logger from '../../logger';
class WorkspaceDirs {
private baseDir: string;
path: string;
other: string;
home: string;
constructor() {
this.baseDir = path.join(vscode.workspace.workspaceFolders![0].uri.path, '..');
this.path = path.join(this.baseDir, 'workspace');
this.other = path.join(this.baseDir, 'other');
this.home = path.join(this.baseDir, 'home');
process.env.HOME = this.home;
}
async teardown() {
}
async setFfxPath(test: Mocha.Context, newPath: string, tools: ToolFinder) {
// first, register to be notified when the config changes
// (needa do this before changing it so we don't race)...
let didChange = anEventOf(tools.onDidUpdateFfx, (evt) => !evt.isInitial);
// ...then, change the config...
await noTimeout(test, vscode.workspace.getConfiguration('fuchsia').update('ffxPath', newPath));
// ... and finally wait till we register that it changes
await didChange;
}
get extFfxPath() {
return path.join(this.other, 'tools', 'x64', 'ffx');
}
get defaultFfx() {
return path.join(this.path, 'tools', 'ffx');
}
}
describe('ToolFinder', function () {
let workspace: WorkspaceDirs;
let tools: ToolFinder;
before(function () {
workspace = new WorkspaceDirs();
const log = vscode.window.createOutputChannel('tool_finder.test', { log: true });
logger.initLogger(log);
});
after(async function () {
await workspace.teardown();
});
beforeEach(async function () {
// reset before creating to simulate a blank slate
// (change config, and wait to be notified that it changed)
if (vscode.workspace.getConfiguration('fuchsia').get('ffxPath')) {
// (only reset if it's set to speed things up)
let didChange = anEventOf(vscode.workspace.onDidChangeConfiguration, (evt) => {
return vscode.workspace.getConfiguration('fuchsia').get('ffxPath') === '';
});
await noTimeout(
this,
vscode.workspace.getConfiguration('fuchsia').update('ffxPath', ''),
);
await separateTimeoutBudget(this, didChange, 5000);
}
tools = new ToolFinder();
});
describe('for FFX', function () {
it('should accept present absolute paths', async function () {
await workspace.setFfxPath(this, workspace.extFfxPath, tools);
assert.strictEqual(tools.ffxPath, workspace.extFfxPath);
});
it('should reject non-existing absolute paths', async function () {
await workspace.setFfxPath(this, '/this/path/is/unknown/ffx', tools);
assert.strictEqual(tools.ffxPath, undefined);
});
it('should set the initial path automatically', async function () {
// Test that ffxpath is auto-set to FFX_PATH_AUTO and the relative path saved in the
// configuration.
// gotta wait till we see that we went through the trouble of updating it first
await anEventOf(tools.onDidUpdateFfx, (evt) => evt.isInitial);
assert.strictEqual(tools.ffxPath, workspace.defaultFfx);
});
it('should reject non-file paths', async function () {
// this sets it to a folder, which is not a file, and therefore doesn't work
await workspace.setFfxPath(this, path.dirname(workspace.extFfxPath), tools);
assert.strictEqual(tools.ffxPath, undefined);
});
it('should reject non-executable paths', async function () {
// this sets it to a folder, which is not a file, and therefore doesn't work
await workspace.setFfxPath(this, path.join(path.dirname(workspace.extFfxPath), 'not-ffx'), tools);
assert.strictEqual(tools.ffxPath, undefined);
});
it('should expand ~/ to $HOME', async function () {
await workspace.setFfxPath(this, '~/fuchsia-sdk/tools/x64/ffx', tools);
assert.strictEqual(tools.ffxPath, `${workspace.home}/fuchsia-sdk/tools/x64/ffx`);
});
it('should not expand ~foo/ to anything', async function () {
await workspace.setFfxPath(this, '~foo/tools/x64/ffx', tools);
assert.strictEqual(tools.ffxPath, `${workspace.path}/~foo/tools/x64/ffx`);
});
it('should accept present relative paths starting with ./', async function () {
// leading ./
await workspace.setFfxPath(this, './tools/x64/ffx', tools);
assert.strictEqual(tools.ffxPath, `${workspace.path}/tools/x64/ffx`);
});
it('should accept present relative paths starting with a directory name', async function () {
await workspace.setFfxPath(this, 'tools/x64/ffx', tools);
assert.strictEqual(tools.ffxPath, `${workspace.path}/tools/x64/ffx`);
});
});
});