summaryrefslogtreecommitdiff
path: root/extension/src/activation/test/environmentSetup.test.ts
blob: ca4874162341ff6aa02a56e621f8b4dd33ca2e26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const child_process = require("child_process");
import { platform } from "os";
import { getPythonPipCommands } from "../environmentSetup";

jest.mock("os");
jest.mock("child_process");

function mockPythonVersionMappings(mappings: { [pythonCmd: string]: string }) {
  (child_process.exec as jest.Mock).mockImplementation(
    (command: string, options: any) => {
      const pythonCmd = command.split(" ")[0];
      if (pythonCmd in mappings) {
        return Promise.resolve([mappings[pythonCmd], ""]);
      } else {
        return Promise.resolve(["", stubStderr]);
      }
    }
  );
}

const stubStderr =
  "This is a stub stderr, but will be checked only for existence.";
describe("getPythonPipCommands", () => {
  describe("on Windows", () => {
    it("should return the correct Python and Pip commands", async () => {
      (platform as jest.Mock).mockReturnValue("win32");
      mockPythonVersionMappings({
        python: "Python 3.8.0",
      });

      const [pythonCmd, pipCmd] = await getPythonPipCommands();

      expect(pythonCmd).toBe("python");
      expect(pipCmd).toBe("pip");

      jest.restoreAllMocks();
    });
    describe("on MacOS", () => {
      (platform as jest.Mock).mockReturnValue("darwin");
      it("should check through all python versions after finding 3.7", async () => {
        mockPythonVersionMappings({
          python: "",
          python3: "Python 3.7.0",
          "python3.11": "Python 3.11.0",
        });

        const [pythonCmd, pipCmd] = await getPythonPipCommands();

        expect(pythonCmd).toBe("python3.11");
        expect(pipCmd).toBe("pip3.11");

        jest.restoreAllMocks();
      });

      it("should use python3 if that maps to valid version", async () => {
        mockPythonVersionMappings({
          python: "",
          python3: "Python 3.8.0",
        });

        const [pythonCmd, pipCmd] = await getPythonPipCommands();

        expect(pythonCmd).toBe("python3");
        expect(pipCmd).toBe("pip3");

        jest.restoreAllMocks();
      });
    });
  });
});