|
| 1 | +package workbench |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "path/filepath" |
| 9 | + "runtime" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/1lck/Lithe-IDEA/desktop/internal/core" |
| 15 | + "github.com/1lck/Lithe-IDEA/desktop/internal/i18n" |
| 16 | + "github.com/1lck/Lithe-IDEA/desktop/internal/store" |
| 17 | + "github.com/1lck/Lithe-IDEA/desktop/internal/toolchain" |
| 18 | +) |
| 19 | + |
| 20 | +func TestPythonSupportPluginRunCurrentFile(t *testing.T) { |
| 21 | + checkPythonCurrentFileRun(t, false) |
| 22 | +} |
| 23 | + |
| 24 | +func TestPythonSupportPluginRunCurrentFileInterpreterOverride(t *testing.T) { |
| 25 | + checkPythonCurrentFileRun(t, true) |
| 26 | +} |
| 27 | + |
| 28 | +func checkPythonCurrentFileRun(t *testing.T, override bool) { |
| 29 | + t.Helper() |
| 30 | + archive := pythonArchiveForE2E(t) |
| 31 | + ctx, cancel := context.WithTimeout(t.Context(), time.Minute) |
| 32 | + defer cancel() |
| 33 | + root, err := filepath.EvalSymlinks(t.TempDir()) |
| 34 | + if err != nil { |
| 35 | + t.Fatal(err) |
| 36 | + } |
| 37 | + venv := filepath.Join(root, ".venv") |
| 38 | + python := "python3" |
| 39 | + if runtime.GOOS == "windows" { |
| 40 | + python = "python" |
| 41 | + } |
| 42 | + if output, err := exec.CommandContext(ctx, python, "-m", "venv", "--without-pip", venv).CombinedOutput(); err != nil { |
| 43 | + t.Fatalf("create project environment: %v\n%s", err, output) |
| 44 | + } |
| 45 | + source := filepath.Join(root, "main.py") |
| 46 | + for name, contents := range map[string]string{ |
| 47 | + "pyproject.toml": "[project]\nname = 'current-file-run'\nversion = '0.1.0'\n", |
| 48 | + "main.py": "import sys\n\nif __name__ == '__main__':\n print('current file ran')\n print(sys.prefix)\n", |
| 49 | + } { |
| 50 | + if err := os.WriteFile(filepath.Join(root, name), []byte(contents), 0o600); err != nil { |
| 51 | + t.Fatal(err) |
| 52 | + } |
| 53 | + } |
| 54 | + kernel := core.NewClient() |
| 55 | + preferences := store.At(t.TempDir()) |
| 56 | + // A headless test has no AppKit event loop to query the system appearance. |
| 57 | + if err := preferences.Save(settingsFile, settings{Theme: themeDark}); err != nil { |
| 58 | + t.Fatal(err) |
| 59 | + } |
| 60 | + // The toolchain resolver reads persisted trust before probing a project .venv. |
| 61 | + trust, err := toolchain.ReadWorkspaceTrust(ctx, kernel, preferences.Dir(), root) |
| 62 | + if err != nil { |
| 63 | + t.Fatal(err) |
| 64 | + } |
| 65 | + if _, err := trust.Save(ctx, kernel, true, false); err != nil { |
| 66 | + t.Fatal(err) |
| 67 | + } |
| 68 | + wantVenv := venv |
| 69 | + if override { |
| 70 | + overrideDir, err := filepath.EvalSymlinks(t.TempDir()) |
| 71 | + if err != nil { |
| 72 | + t.Fatal(err) |
| 73 | + } |
| 74 | + wantVenv = filepath.Join(overrideDir, "configured interpreter") |
| 75 | + if output, err := exec.CommandContext(ctx, python, "-m", "venv", "--without-pip", wantVenv).CombinedOutput(); err != nil { |
| 76 | + t.Fatalf("create configured environment: %v\n%s", err, output) |
| 77 | + } |
| 78 | + configured := filepath.Join(wantVenv, "bin", "python") |
| 79 | + if runtime.GOOS == "windows" { |
| 80 | + configured = filepath.Join(wantVenv, "Scripts", "python.exe") |
| 81 | + } |
| 82 | + settings, err := json.Marshal(map[string]map[string]string{ |
| 83 | + pythonSupportPluginID: {"pythonExecutable": configured}, |
| 84 | + }) |
| 85 | + if err != nil { |
| 86 | + t.Fatal(err) |
| 87 | + } |
| 88 | + if err := os.WriteFile(filepath.Join(preferences.Dir(), "plugin-settings.json"), settings, 0o600); err != nil { |
| 89 | + t.Fatal(err) |
| 90 | + } |
| 91 | + } |
| 92 | + h := newHarnessWithOptions(t, Options{ |
| 93 | + Kernel: kernel, PluginKernel: kernel, PluginRoot: t.TempDir(), Store: preferences, |
| 94 | + Languages: []string{"en"}, |
| 95 | + }) |
| 96 | + installPythonArchive(t, ctx, h.app.plugins, archive) |
| 97 | + h.app.reloadPluginLanguages() |
| 98 | + h.open(root) |
| 99 | + p := h.app.project |
| 100 | + if p == nil { |
| 101 | + t.Fatal("the workspace did not open") |
| 102 | + } |
| 103 | + openSource(t, h, p, source) |
| 104 | + h.app.post(func(a *App) { |
| 105 | + a.chooseTitleConfiguration(p, "") |
| 106 | + a.titleRun(p) |
| 107 | + }) |
| 108 | + notRunnable := i18n.Shared(h.app.language(), "desktop.title.currentFileNotRunnable") |
| 109 | + awaitStateWithin(t, h, 20*time.Second, "the current file to start running", func() bool { |
| 110 | + for _, notice := range noticeTexts(h.app) { |
| 111 | + if notice == notRunnable { |
| 112 | + t.Fatalf("the toolbar rejected the Python entrypoint: %s", notice) |
| 113 | + } |
| 114 | + } |
| 115 | + return p.runConfigs.selected != "" |
| 116 | + }) |
| 117 | + run := awaitProcessRun(t, h, p, "main.py", nil) |
| 118 | + checkOutput := func(output string) { |
| 119 | + t.Helper() |
| 120 | + if !strings.Contains(output, "current file ran") || !strings.Contains(output, wantVenv) { |
| 121 | + t.Fatalf("expected the program to use Python environment %q:\n%s", wantVenv, output) |
| 122 | + } |
| 123 | + } |
| 124 | + checkOutput(run.output.all()) |
| 125 | + saved := p.runConfigs.configuration(p.runConfigs.selected) |
| 126 | + if saved == nil { |
| 127 | + t.Fatal("the toolbar did not select the saved run configuration") |
| 128 | + } |
| 129 | + if ref, ok := saved.BuildReference(); !ok || ref.Run != "main" || ref.TargetID != "" { |
| 130 | + t.Fatalf("current file saved the wrong build reference: %+v", ref) |
| 131 | + } |
| 132 | + |
| 133 | + for i := range p.build.instances { |
| 134 | + instance := &p.build.instances[i] |
| 135 | + if instance.BuildSystemID != "python" { |
| 136 | + continue |
| 137 | + } |
| 138 | + for _, target := range instance.Model.Targets { |
| 139 | + if target.Name == "main.py" { |
| 140 | + action, ok := buildPrimaryAction(instance, &target) |
| 141 | + if !ok || action.entryID == "" { |
| 142 | + t.Fatal("main.py no longer has a command run in the Build panel") |
| 143 | + } |
| 144 | + h.app.post(func(a *App) { |
| 145 | + a.selectBuildInstance(p, i) |
| 146 | + a.runBuildTargetAction(p, target.ID, action) |
| 147 | + }) |
| 148 | + checkOutput(awaitProcessRun(t, h, p, "main.py", run).output.all()) |
| 149 | + return |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + t.Fatal("the build model no longer exposes the main.py target") |
| 154 | +} |
0 commit comments