mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-11 10:38:31 -05:00
* Fix workspaceRoot macro for insights
The workspaceRoot macro wasn't working correctly for finding the queryFile. There were a couple of issues :
1. The path separators were hardcoded as / which wasn't xplat-compatible
2. They required the first section of the path was one of the folders in the workspace - e.g. if the workspace contained a folder named foo you'd have to specify ${workspaceRoot}\foo\myfile.sql. This is inconsistent with the folder logich was just appends the path after ${workspaceRoot} to the folder that's currently open
I changed the logic to just append the relative part of the path to every folder currently open in the workspace and choose the first one that it found that contained the file we were told to look for - which follows the convention the folder logic uses. If the file doesn't exist it'll just fall back to using the path without the macro (which is likely to not resolve and thus will display an error, but there's nothing we can do at that point anyways)
* Switch to using VS Code resolver (support for more than just workspaceRoot) and move resolution code into helper method so it can be used by the multiple places it's called. Added tests for the methods.
* Add test for invalid param
* Change resolveQueryFilePath to be a standalone exported function. Change it to throw if the file can't be resolved/found so callers can display error correctly. Added more tests to covery new scenarios. Switch to using pfs instead of fs for file existance checks.
* Add extra param to InsightsDialogController construction in test
* Fix formatting and test errors.
* Change to suiteSetup and suiteTeardown so the setup/teardown is only ran once instead of once per test - we don't need unique files and this stops a race condition error with deleting the test folder.
* spaces -> tabs
42 lines
2.2 KiB
TypeScript
42 lines
2.2 KiB
TypeScript
/*---------------------------------------------------------------------------------------------
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
|
*--------------------------------------------------------------------------------------------*/
|
|
|
|
import * as pfs from 'vs/base/node/pfs';
|
|
import { localize } from 'vs/nls';
|
|
|
|
import { IWorkspaceContextService, IWorkspaceFolder } from 'vs/platform/workspace/common/workspace';
|
|
import { IConfigurationResolverService } from 'vs/workbench/services/configurationResolver/common/configurationResolver';
|
|
|
|
/**
|
|
* Resolves the given file path using the VS ConfigurationResolver service, replacing macros such as
|
|
* ${workspaceRoot} with their expected values and then testing each path to see if it exists. It will
|
|
* return either the first full path that exists or throw an error if none of the resolved paths exist
|
|
* @param filePath The path to resolve
|
|
* @param workspaceContextService The workspace context to use for resolving workspace vars
|
|
* @param configurationResolverService The resolver service to use to resolve the vars
|
|
*/
|
|
export async function resolveQueryFilePath(filePath: string,
|
|
workspaceContextService: IWorkspaceContextService,
|
|
configurationResolverService: IConfigurationResolverService): Promise<string> {
|
|
if (!filePath || !workspaceContextService || !configurationResolverService) {
|
|
return filePath;
|
|
}
|
|
|
|
let workspaceFolders: IWorkspaceFolder[] = workspaceContextService.getWorkspace().folders;
|
|
// Resolve the path using each folder in our workspace, or undefined if there aren't any
|
|
// (so that non-folder vars such as environment vars still resolve)
|
|
let resolvedFilePaths = (workspaceFolders.length > 0 ? workspaceFolders : [undefined])
|
|
.map(f => configurationResolverService.resolve(f, filePath));
|
|
|
|
// Just need a single query file so use the first we find that exists
|
|
for (const path of resolvedFilePaths) {
|
|
if (await pfs.exists(path)) {
|
|
return path;
|
|
}
|
|
}
|
|
|
|
throw Error(localize('insightsDidNotFindResolvedFile', 'Could not find query file at any of the following paths :\n {0}', resolvedFilePaths.join('\n')));
|
|
}
|