forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathunittests.js
More file actions
1 lines (1 loc) · 5.56 KB
/
Copy pathunittests.js
File metadata and controls
1 lines (1 loc) · 5.56 KB
1
define(function(require,exports,module){if(!Phoenix.isNativeApp)return;const SpecRunnerUtils=brackets.getModule("spec/SpecRunnerUtils");describe("integration:Python LSP",function(){const testFolder=SpecRunnerUtils.getTestPath("/spec/PythonSupport-test-files");let testWindow,$,CommandManager,Commands,EditorManager,QuickViewManager;function _pyModule(name){return new Promise(function(resolve,reject){const ExtensionLoader=testWindow.brackets.test.ExtensionLoader,ctx=ExtensionLoader.getRequireContextForExtension("PythonSupport");ctx([name],resolve,reject)})}async function _openFile(fileName){await awaitsForDone(CommandManager.execute(Commands.CMD_ADD_TO_WORKINGSET_AND_OPEN,{fullPath:testFolder+"/"+fileName}))}function _problemsText(){return $("#problems-panel").text()}beforeAll(async function(){testWindow=await SpecRunnerUtils.createTestWindowAndRun(),$=testWindow.$,CommandManager=testWindow.brackets.test.CommandManager,Commands=testWindow.brackets.test.Commands,EditorManager=testWindow.brackets.test.EditorManager,QuickViewManager=testWindow.brackets.getModule("features/QuickViewManager"),await SpecRunnerUtils.loadProjectInTestWindow(testFolder);const ServerInstaller=await _pyModule("ServerInstaller"),result=await ServerInstaller.installNow();if(!result)throw new Error("Pyrefly install failed - network needed for the first run");await _openFile("error.py"),await awaitsFor(function(){return/python \(/.test(_problemsText())},"first pyrefly diagnostics in the problems panel",12e4)},24e4),afterAll(async function(){testWindow=null,await SpecRunnerUtils.closeTestWindow()},3e4),it("should report Python type errors from the language server",async function(){await _openFile("error.py"),await awaitsFor(function(){return/python \(/.test(_problemsText())&&/undefined_name_here|not assignable/i.test(_problemsText())},"python type diagnostic",3e4)},45e3),it("should mark the python linting provider active (and cpp untouched by it)",async function(){const LSPClient=testWindow.brackets.getModule("languageTools/LSPClient");expect(LSPClient.isLintingProviderActive("python")).toBe(!0),expect(LSPClient.isLintingProviderActive("cpp")).toBe(!1)},45e3),it("should show hover docs for a user-defined function",async function(){await _openFile("funcs.py");const editor=EditorManager.getActiveEditor(),lines=editor.document.getText().split("\n"),callLine=lines.findIndex(function(l){return-1!==l.indexOf("print(compute_total")}),pos={line:callLine,ch:lines[callLine].indexOf("compute_total")+3};let hoverText=null;await awaitsFor(function(){return QuickViewManager._queryPreviewProviders(editor,pos,editor.getToken(pos)).then(function(popover){popover&&popover.content&&(hoverText=$(popover.content).text())}).then(function(){return hoverText&&-1!==hoverText.indexOf("compute_total")})},"hover docs for compute_total",3e4),expect(hoverText).toContain("compute_total")},45e3),it("should jump to definition within the file",async function(){await _openFile("funcs.py");const editor=EditorManager.getActiveEditor(),lines=editor.document.getText().split("\n"),callLine=lines.findIndex(function(l){return-1!==l.indexOf("print(compute_total")}),defLine=lines.findIndex(function(l){return-1!==l.indexOf("def compute_total")});editor.setCursorPos({line:callLine,ch:lines[callLine].indexOf("compute_total")+3}),await awaitsForDone(CommandManager.execute(Commands.NAVIGATE_JUMPTO_DEFINITION)),await awaitsFor(function(){return EditorManager.getActiveEditor().getCursorPos().line===defLine},"cursor to land on the definition",3e4)},45e3),it("should offer pyrefly completions for a partial identifier",async function(){await _openFile("funcs.py");const editor=EditorManager.getActiveEditor(),lines=editor.document.getText().split("\n"),callLine=lines.findIndex(function(l){return-1!==l.indexOf("print(compute_total")});editor.setCursorPos({line:callLine,ch:lines[callLine].indexOf("compute_total")+8});const pyMain=await _pyModule("main"),provider=pyMain._getClient().codeHints;let hintTexts=null;await awaitsFor(function(){if(!provider.hasHints(editor,null))return!1;const response=provider.getHints(null);return!!response&&(response.done(function(result){hintTexts=(result&&result.hints||[]).map(function($item){return $item.text?$item.text():String($item)})}),hintTexts&&-1!==hintTexts.join("|").indexOf("compute_total"))},"compute_total in pyrefly completions",3e4)},45e3),it("should attach the server's quickfix with its title on unknown-name diagnostics",async function(){await _openFile("error.py");const pyMain=await _pyModule("main"),provider=pyMain._getClient().lintingProvider,editor=EditorManager.getActiveEditor(),filePath=editor.document.file.fullPath;let fix=null;await awaitsFor(function(){const results=provider.getInspectionResults(null,filePath),errors=results&&results.errors||[],typo=errors.find(function(err){return err.message&&-1!==err.message.indexOf("`valu`")});return!!(fix=typo&&typo.fix)},"server quickfix on the `valu` diagnostic",3e4),expect(typeof fix.replaceText).toBe("string"),expect(fix.title).toContain("valu")},45e3),it("should format python through the standalone ruff Beautify provider",async function(){const Beautifier=await _pyModule("Beautifier"),ugly="x=1\ndef f( a,b ):\n return a+b\n",result=await Beautifier.beautifyTextProvider(ugly,"scratch.py");expect(result.originalText).toBe(ugly),expect(result.changedText).toContain("x = 1"),expect(result.changedText).toContain("def f(a, b):"),expect(result.changedText).toContain("return a + b")},45e3),it("should never auto-install in a test window",async function(){const ServerInstaller=await _pyModule("ServerInstaller"),result=await ServerInstaller.autoInstall();expect(result).toBe(null)},45e3)})});