Skip to content

Commit 7c41bad

Browse files
committed
Remove all activation logic from this extension
Other extension should be the ones chosing when to activate it.
1 parent ff6a54b commit 7c41bad

File tree

6 files changed

+80
-44
lines changed

6 files changed

+80
-44
lines changed

package.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@
1515
"categories": [
1616
"Programming Languages"
1717
],
18-
"activationEvents": [
19-
"onLanguage:ruby",
20-
"workspaceContains:Gemfile",
21-
"workspaceContains:gems.rb"
22-
],
18+
"activationEvents": [],
2319
"main": "./dist/extension.js",
2420
"contributes": {
2521
"commands": [

src/extension.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import * as vscode from "vscode";
22
import { RubyEnvironment, RubyEnvironmentsApi } from "./rubyEnvironment";
33

4-
export async function activate(context: vscode.ExtensionContext): Promise<RubyEnvironmentsApi> {
4+
export function activate(context: vscode.ExtensionContext): RubyEnvironmentsApi {
55
const outputChannel = vscode.window.createOutputChannel("Ruby Environments", { log: true });
66
context.subscriptions.push(outputChannel);
77

88
const rubyEnvironment = new RubyEnvironment(context, outputChannel);
9-
await rubyEnvironment.activate();
109
return rubyEnvironment;
1110
}
1211

src/rubyEnvironment.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,20 @@ export interface RubyEnvironmentsApi {
1616
* Main class that manages the Ruby environment state and lifecycle
1717
*/
1818
export class RubyEnvironment implements RubyEnvironmentsApi {
19-
private versionManager: VersionManager;
19+
private versionManager: VersionManager | null = null;
2020
private currentRubyDefinition: RubyDefinition | null = null;
21+
private workspaceFolder: vscode.WorkspaceFolder | undefined;
22+
private status: RubyStatus | null = null;
23+
2124
private readonly logger: vscode.LogOutputChannel;
2225
private readonly context: vscode.ExtensionContext;
23-
private readonly workspaceFolder: vscode.WorkspaceFolder | undefined;
24-
private readonly status: RubyStatus;
2526

2627
constructor(context: vscode.ExtensionContext, logger: vscode.LogOutputChannel) {
2728
this.context = context;
2829
this.logger = logger;
30+
}
31+
32+
async activate(): Promise<void> {
2933
this.workspaceFolder = vscode.workspace.workspaceFolders?.[0];
3034

3135
this.logger.info("Ruby Environments extension activating...");
@@ -41,14 +45,12 @@ export class RubyEnvironment implements RubyEnvironmentsApi {
4145

4246
// Create the status item
4347
this.status = new RubyStatus();
44-
context.subscriptions.push(this.status);
48+
this.context.subscriptions.push(this.status);
4549

4650
// Setup watchers and commands
4751
this.setupConfigWatcher();
4852
this.registerCommands();
49-
}
5053

51-
async activate(): Promise<void> {
5254
this.logger.info("Activating Ruby environment...");
5355
this.currentRubyDefinition = await this.versionManager.activate();
5456

src/test/extension.test.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,39 @@
11
import * as assert from "assert";
2-
import { suite, test } from "mocha";
3-
import { deactivate } from "../extension";
2+
import { suite, test, beforeEach, afterEach } from "mocha";
3+
import { activate, deactivate } from "../extension";
4+
import { RubyEnvironmentsApi } from "../rubyEnvironment";
5+
import { FakeContext, createContext } from "./helpers";
46

57
suite("Extension Test Suite", () => {
8+
suite("activate", () => {
9+
let context: FakeContext;
10+
11+
beforeEach(() => {
12+
context = createContext();
13+
});
14+
15+
afterEach(() => {
16+
context.dispose();
17+
});
18+
19+
test("returns an object implementing RubyEnvironmentsApi", () => {
20+
const api = activate(context);
21+
22+
// Verify the returned object has the required API methods
23+
assert.strictEqual(typeof api, "object", "activate should return an object");
24+
assert.strictEqual(typeof api.activate, "function", "API should have an activate method");
25+
assert.strictEqual(typeof api.getRuby, "function", "API should have a getRuby method");
26+
});
27+
28+
test("returned API conforms to RubyEnvironmentsApi interface", () => {
29+
const api = activate(context);
30+
31+
// Type assertion to ensure the return value conforms to the interface
32+
const typedApi: RubyEnvironmentsApi = api;
33+
assert.ok(typedApi, "API should conform to RubyEnvironmentsApi interface");
34+
});
35+
});
36+
637
suite("deactivate", () => {
738
test("can be called without errors", () => {
839
assert.doesNotThrow(() => {

src/test/helpers.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import * as vscode from "vscode";
2+
3+
export type FakeContext = vscode.ExtensionContext & { dispose: () => void };
4+
5+
export function createContext(): FakeContext {
6+
const subscriptions: vscode.Disposable[] = [];
7+
8+
return {
9+
subscriptions,
10+
dispose: () => {
11+
subscriptions.forEach((subscription) => {
12+
subscription.dispose();
13+
});
14+
},
15+
} as unknown as FakeContext;
16+
}

src/test/rubyEnvironment.test.ts

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,9 @@ import * as assert from "assert";
22
import { suite, test, beforeEach, afterEach } from "mocha";
33
import * as vscode from "vscode";
44
import { RubyEnvironment } from "../rubyEnvironment";
5+
import { FakeContext, createContext } from "./helpers";
56

67
suite("RubyEnvironment Test Suite", () => {
7-
type FakeContext = vscode.ExtensionContext & { dispose: () => void };
8-
9-
function createContext() {
10-
const subscriptions: vscode.Disposable[] = [];
11-
12-
return {
13-
subscriptions,
14-
dispose: () => {
15-
subscriptions.forEach((subscription) => {
16-
subscription.dispose();
17-
});
18-
},
19-
} as unknown as FakeContext;
20-
}
21-
228
function createMockLogger(): vscode.LogOutputChannel {
239
return {
2410
info: () => {},
@@ -50,14 +36,27 @@ suite("RubyEnvironment Test Suite", () => {
5036
assert.strictEqual(typeof rubyEnvironment.getRuby, "function", "getRuby should be a function");
5137
});
5238

53-
test("registers config watcher, status item, and command subscriptions", () => {
54-
new RubyEnvironment(context, mockLogger);
39+
suite("activate", () => {
40+
test("registers config watcher, status item, and command subscriptions", async () => {
41+
const rubyEnvironment = new RubyEnvironment(context, mockLogger);
42+
43+
await rubyEnvironment.activate();
5544

56-
assert.strictEqual(
57-
context.subscriptions.length,
58-
3,
59-
"Extension should register three subscriptions (status item, config watcher, and command)",
60-
);
45+
assert.strictEqual(
46+
context.subscriptions.length,
47+
3,
48+
"Extension should register three subscriptions (status item, config watcher, and command)",
49+
);
50+
});
51+
52+
test("registers selectRubyVersion command", async () => {
53+
const rubyEnvironment = new RubyEnvironment(context, mockLogger);
54+
55+
await rubyEnvironment.activate();
56+
57+
const commands = await vscode.commands.getCommands(true);
58+
assert.ok(commands.includes("ruby-environments.selectRubyVersion"), "Command should be registered");
59+
});
6160
});
6261

6362
test("returns initial Ruby definition from configuration", () => {
@@ -68,12 +67,5 @@ suite("RubyEnvironment Test Suite", () => {
6867
// Since no configuration is set in tests, it should return null
6968
assert.strictEqual(result, null, "getRuby should return null when no configuration is set");
7069
});
71-
72-
test("registers selectRubyVersion command", async () => {
73-
new RubyEnvironment(context, mockLogger);
74-
75-
const commands = await vscode.commands.getCommands(true);
76-
assert.ok(commands.includes("ruby-environments.selectRubyVersion"), "Command should be registered");
77-
});
7870
});
7971
});

0 commit comments

Comments
 (0)