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

Tests for project configuration #39

Merged
merged 5 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 65 additions & 34 deletions src/commands/configureProjectCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ export interface ProjectConfigurationProcessor {
): void;
getProjectFolderPath(): Promise<Uri[] | undefined>;
preActionUserAcknowledgment(): Promise<void>;
executeProjectCreation(folderUri: Uri): Promise<string>;
executeProjectOpen(folderUri: Uri): Promise<void>;
}

class DefaultProjectConfigurationProcessor
export class DefaultProjectConfigurationProcessor
implements ProjectConfigurationProcessor
{
extensionUri: Uri;

constructor(extensionUri: Uri) {
this.extensionUri = extensionUri;
}
Expand All @@ -49,6 +52,26 @@ class DefaultProjectConfigurationProcessor
});
}

async executeProjectCreation(folderUri: Uri): Promise<string> {
return new Promise(async (resolve) => {
await commands.executeCommand(
'git.clone',
ConfigureProjectCommand.STARTER_KIT_REPO_URI,
folderUri.fsPath
);
return resolve(folderUri.fsPath);
});
}

async executeProjectOpen(folderUri: Uri): Promise<void> {
return new Promise(async (resolve) => {
await commands.executeCommand('vscode.openFolder', folderUri, {
forceReuseWindow: true
});
return resolve();
});
}

async getProjectFolderPath(): Promise<Uri[] | undefined> {
return new Promise((resolve) => {
window
Expand Down Expand Up @@ -110,24 +133,44 @@ export class ConfigureProjectCommand {
new DefaultProjectConfigurationProcessor(extensionUri);
}

async createProjectAction(
panel?: WebviewPanel
): Promise<string | undefined> {
return new Promise((resolve) => {
// It's actually important to run this async, because
// createNewProject() will not resolve its Promise
// until a path is selected, allowing the user to
// cancel the open dialog and re-initiate it as many
// times as they want.
this.createNewProject(panel).then((path) => {
return resolve(path);
});
});
}

async openProjectAction(panel?: WebviewPanel): Promise<string | undefined> {
return new Promise((resolve) => {
// It's actually important to run this async, because
// createNewProject() will not resolve its Promise
// until a path is selected, allowing the user to
// cancel the open dialog and re-initiate it as many
// times as they want.
this.openExistingProject(panel).then((path) => {
return resolve(path);
});
});
}

async configureProject(): Promise<string | undefined> {
return new Promise(async (resolve) => {
return new Promise((resolve) => {
this.projectConfigurationProcessor.getProjectManagementChoice(
(panel) => {
// It's actually important to run this async, because
// createNewProject() will not resolve its Promise
// until a path is selected, allowing the user to
// cancel the open dialog and re-initiate it as many
// times as they want.
this.createNewProject(panel).then((path) => {
return resolve(path);
});
async (panel) => {
const path = await this.createProjectAction(panel);
return resolve(path);
},
(panel) => {
// See above for rationale for running this async.
this.openExistingProject(panel).then((path) => {
return resolve(path);
});
async (panel) => {
const path = await this.openProjectAction(panel);
return resolve(path);
}
);
});
Expand All @@ -153,9 +196,10 @@ export class ConfigureProjectCommand {
.preActionUserAcknowledgment()
.then(async () => {
try {
const path = await this.executeProjectCreation(
folderUri[0]
);
const path =
await this.projectConfigurationProcessor.executeProjectCreation(
folderUri[0]
);
return resolve(path);
} catch (error) {
return reject(error);
Expand Down Expand Up @@ -192,27 +236,14 @@ export class ConfigureProjectCommand {
this.projectConfigurationProcessor
.preActionUserAcknowledgment()
.then(async () => {
await commands.executeCommand(
'vscode.openFolder',
folderUri[0],
{ forceReuseWindow: true }
await this.projectConfigurationProcessor.executeProjectOpen(
folderUri[0]
);
return resolve(folderUri[0].fsPath);
});
});
}

async executeProjectCreation(folderUri: Uri): Promise<string> {
return new Promise(async (resolve) => {
await commands.executeCommand(
'git.clone',
ConfigureProjectCommand.STARTER_KIT_REPO_URI,
folderUri.fsPath
);
return resolve(folderUri.fsPath);
});
}

async validateProjectFolder(projectFolderUri: Uri): Promise<void> {
return new Promise(async (resolve, reject) => {
const origWorkingDir = process.cwd();
Expand Down
12 changes: 10 additions & 2 deletions src/test/TestHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@ export class TempProjectDirManager {
}

// Create a platform-agnostic absolute path to a non-existent folder.
export function createNonExistentAbsolutePath(): string {
export function createPlatformAbsolutePath(...pathArgs: string[]): string {
const topLevel = path.parse(process.cwd()).root;
return path.join(topLevel, 'starter-kit-tests', 'path', 'to', 'nowhere');
let absPath = path.join(topLevel, ...pathArgs);

// On Windows, Uri.fsPath normalizes the drive letter down to lower-case.
// If we don't do the same, tests will break.
if (process.platform.startsWith('win')) {
const firstChar = absPath.charAt(0).toLowerCase();
absPath = firstChar + absPath.slice(1);
}
return absPath;
}
Loading