Skip to content

Commit 5502e4c

Browse files
committed
Remove all activation logic from this extension
Other extension should be the ones chosing when to activate it.
1 parent 6dc5b29 commit 5502e4c

5 files changed

Lines changed: 64 additions & 44 deletions

File tree

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
@@ -2,12 +2,11 @@ import * as vscode from "vscode";
22
import { RubyEnvironmentsApi } from "./types";
33
import { RubyEnvironment } from "./rubyEnvironment";
44

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

99
const rubyEnvironment = new RubyEnvironment(context, outputChannel);
10-
await rubyEnvironment.activate();
1110
return rubyEnvironment;
1211
}
1312

src/rubyEnvironment.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,22 @@ import { VersionManager } from "./versionManager";
88
* Main class that manages the Ruby environment state and lifecycle
99
*/
1010
export class RubyEnvironment implements RubyEnvironmentsApi {
11-
private versionManager: VersionManager;
11+
private versionManager: VersionManager | null = null;
1212
private currentRubyDefinition: RubyDefinition | null = null;
13+
private workspaceFolder: vscode.WorkspaceFolder | undefined;
14+
private status: RubyStatus | null = null;
15+
1316
private readonly logger: vscode.LogOutputChannel;
1417
private readonly context: vscode.ExtensionContext;
15-
private readonly workspaceFolder: vscode.WorkspaceFolder | undefined;
16-
private readonly status: RubyStatus;
1718
// Event emitter for Ruby environment changes
1819
private readonly rubyChangeEmitter = new vscode.EventEmitter<RubyChangeEvent>();
1920

2021
constructor(context: vscode.ExtensionContext, logger: vscode.LogOutputChannel) {
2122
this.context = context;
2223
this.logger = logger;
24+
}
25+
26+
async activate(): Promise<void> {
2327
this.workspaceFolder = vscode.workspace.workspaceFolders?.[0];
2428

2529
this.logger.info("Ruby Environments extension activating...");
@@ -35,14 +39,12 @@ export class RubyEnvironment implements RubyEnvironmentsApi {
3539

3640
// Create the status item
3741
this.status = new RubyStatus();
38-
context.subscriptions.push(this.status);
42+
this.context.subscriptions.push(this.status);
3943

4044
// Setup watchers and commands
4145
this.setupConfigWatcher();
4246
this.registerCommands();
43-
}
4447

45-
async activate(): Promise<void> {
4648
this.logger.info("Activating Ruby environment...");
4749
this.currentRubyDefinition = await this.versionManager.activate();
4850

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 { FakeContext, createContext } from "./helpers";
5+
import { RubyEnvironmentsApi } from "../types";
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/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)