Skip to content

Commit 1b824f1

Browse files
committed
Add sane defaults to Start-EditorServices
We do not need to require so many CLI flags. This continues to run the existing Emacs and Vim tests utilizing those flags as a regression scenario, and adds an additional pair of tests that launch PSES with a minimal set of flags.
1 parent 4c39342 commit 1b824f1

File tree

7 files changed

+147
-24
lines changed

7 files changed

+147
-24
lines changed

.github/workflows/emacs-test.yml

+6-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@ jobs:
4141
with:
4242
version: '28.2'
4343

44-
- name: Run ERT
44+
- name: Run ERT with full CLI
4545
run: |
4646
emacs -Q --batch -f package-refresh-contents --eval "(package-install 'eglot)"
4747
emacs -Q --batch -l test/emacs-test.el -f ert-run-tests-batch-and-exit
48+
49+
- name: Run ERT with simple CLI
50+
run: |
51+
emacs -Q --batch -f package-refresh-contents --eval "(package-install 'eglot)"
52+
emacs -Q --batch -l test/emacs-simple-test.el -f ert-run-tests-batch-and-exit

.github/workflows/vim-test.yml

+6-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ jobs:
6161
repository: thinca/vim-themis
6262
path: vim-themis
6363

64-
- name: Run Themis
64+
- name: Run Themis with full CLI
6565
env:
6666
THEMIS_VIM: ${{ steps.vim.outputs.executable }}
6767
run: ./vim-themis/bin/themis ./test/vim-test.vim
68+
69+
- name: Run Themis with simple CLI
70+
env:
71+
THEMIS_VIM: ${{ steps.vim.outputs.executable }}
72+
run: ./vim-themis/bin/themis ./test/vim-simple-test.vim

module/PowerShellEditorServices/Start-EditorServices.ps1

+5-12
Original file line numberDiff line numberDiff line change
@@ -27,35 +27,31 @@
2727
#>
2828
[CmdletBinding(DefaultParameterSetName="NamedPipe")]
2929
param(
30-
[Parameter(Mandatory=$true)]
3130
[ValidateNotNullOrEmpty()]
3231
[string]
33-
$HostName,
32+
$HostName = "PSES",
3433

35-
[Parameter(Mandatory=$true)]
3634
[ValidateNotNullOrEmpty()]
3735
[string]
38-
$HostProfileId,
36+
$HostProfileId = "PSES",
3937

40-
[Parameter(Mandatory=$true)]
4138
[ValidateNotNullOrEmpty()]
4239
[string]
43-
$HostVersion,
40+
$HostVersion = "0.0.0",
4441

4542
[ValidateNotNullOrEmpty()]
4643
[string]
4744
$BundledModulesPath,
4845

4946
[ValidateNotNullOrEmpty()]
50-
$LogPath,
47+
$LogPath = "PowerShellEditorServices.log",
5148

5249
[ValidateSet("Diagnostic", "Verbose", "Normal", "Warning", "Error")]
5350
$LogLevel,
5451

55-
[Parameter(Mandatory=$true)]
5652
[ValidateNotNullOrEmpty()]
5753
[string]
58-
$SessionDetailsPath,
54+
$SessionDetailsPath = "PowerShellEditorServices.json",
5955

6056
[switch]
6157
$EnableConsoleRepl,
@@ -78,9 +74,6 @@ param(
7874
[switch]
7975
$WaitForDebugger,
8076

81-
[switch]
82-
$ConfirmInstall,
83-
8477
[Parameter(ParameterSetName="Stdio", Mandatory=$true)]
8578
[switch]
8679
$Stdio,

src/PowerShellEditorServices.Hosting/Commands/StartEditorServicesCommand.cs

+23-9
Original file line numberDiff line numberDiff line change
@@ -39,33 +39,44 @@ public StartEditorServicesCommand()
3939
Environment.SetEnvironmentVariable("POWERSHELL_DISTRIBUTION_CHANNEL", "PSES");
4040
_disposableResources = new List<IDisposable>();
4141
_loggerUnsubscribers = new List<IDisposable>();
42+
43+
// These are the actual defaults. Because of how PowerShell cmdlets work, the "defaults"
44+
// in the corresponding .PS1 file don't matter (though they match for discoverability),
45+
// nor can these be set at the field declaration because the engine resets parameters to
46+
// null on invocation.
47+
HostName = "PSES";
48+
HostProfileId = "PSES";
49+
HostVersion = new Version(0, 0, 0);
50+
SessionDetailsPath = "PowerShellEditorServices.json";
51+
LogPath = "PowerShellEditorServices.log";
52+
LogLevel = PsesLogLevel.Normal;
4253
}
4354

4455
/// <summary>
4556
/// The name of the EditorServices host to report.
4657
/// </summary>
47-
[Parameter(Mandatory = true)]
58+
[Parameter]
4859
[ValidateNotNullOrEmpty]
4960
public string HostName { get; set; }
5061

5162
/// <summary>
5263
/// The ID to give to the host's profile.
5364
/// </summary>
54-
[Parameter(Mandatory = true)]
65+
[Parameter]
5566
[ValidateNotNullOrEmpty]
5667
public string HostProfileId { get; set; }
5768

5869
/// <summary>
5970
/// The version to report for the host.
6071
/// </summary>
61-
[Parameter(Mandatory = true)]
72+
[Parameter]
6273
[ValidateNotNullOrEmpty]
6374
public Version HostVersion { get; set; }
6475

6576
/// <summary>
6677
/// Path to the session file to create on startup or startup failure.
6778
/// </summary>
68-
[Parameter(Mandatory = true)]
79+
[Parameter]
6980
[ValidateNotNullOrEmpty]
7081
public string SessionDetailsPath { get; set; }
7182

@@ -118,7 +129,7 @@ public StartEditorServicesCommand()
118129
/// <summary>
119130
/// The path to where PowerShellEditorServices and its bundled modules are.
120131
/// </summary>
121-
[Parameter]
132+
[Parameter(Mandatory = true)]
122133
[ValidateNotNullOrEmpty]
123134
public string BundledModulesPath { get; set; }
124135

@@ -133,7 +144,7 @@ public StartEditorServicesCommand()
133144
/// The minimum log level that should be emitted.
134145
/// </summary>
135146
[Parameter]
136-
public PsesLogLevel LogLevel { get; set; } = PsesLogLevel.Normal;
147+
public PsesLogLevel LogLevel { get; set; }
137148

138149
/// <summary>
139150
/// Paths to additional PowerShell modules to be imported at startup.
@@ -287,9 +298,12 @@ private void StartLogging()
287298

288299
private string GetLogDirPath()
289300
{
290-
string logDir = !string.IsNullOrEmpty(LogPath)
291-
? Path.GetDirectoryName(LogPath)
292-
: Path.GetDirectoryName(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
301+
string logDir = Path.GetDirectoryName(LogPath);
302+
if (string.IsNullOrEmpty(logDir))
303+
{
304+
logDir = Directory.GetCurrentDirectory();
305+
// Path.GetDirectoryName(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
306+
}
293307

294308
// Ensure logDir exists
295309
Directory.CreateDirectory(logDir);

test/emacs-simple-test.el

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
;;; emacs-test.el --- Integration testing script -*- lexical-binding: t; -*-
2+
3+
;; Copyright (c) Microsoft Corporation.
4+
;; Licensed under the MIT License.
5+
6+
;; Author: Andy Jordan <[email protected]>
7+
;; Keywords: PowerShell, LSP
8+
9+
;;; Code:
10+
11+
;; Avoid using old packages.
12+
(setq load-prefer-newer t)
13+
14+
;; Improved TLS Security.
15+
(with-eval-after-load 'gnutls
16+
(custom-set-variables
17+
'(gnutls-verify-error t)
18+
'(gnutls-min-prime-bits 3072)))
19+
20+
;; Package setup.
21+
(require 'package)
22+
(add-to-list 'package-archives
23+
'("melpa" . "https://melpa.org/packages/") t)
24+
(package-initialize)
25+
(package-refresh-contents)
26+
27+
(require 'ert)
28+
29+
(require 'flymake)
30+
31+
(unless (package-installed-p 'powershell)
32+
(package-install 'powershell))
33+
(require 'powershell)
34+
35+
(unless (package-installed-p 'eglot)
36+
(package-install 'eglot))
37+
(require 'eglot)
38+
39+
(ert-deftest powershell-editor-services ()
40+
"Eglot should connect to PowerShell Editor Services."
41+
(let* ((repo (project-root (project-current)))
42+
(start-script (expand-file-name "module/PowerShellEditorServices/Start-EditorServices.ps1" repo))
43+
(module-path (expand-file-name "module" repo))
44+
(test-script (expand-file-name "test/PowerShellEditorServices.Test.Shared/Debugging/VariableTest.ps1" repo))
45+
(eglot-sync-connect t))
46+
(add-to-list
47+
'eglot-server-programs
48+
`(powershell-mode
49+
. ("pwsh" "-NoLogo" "-NoProfile" "-Command" ,start-script
50+
"-BundledModulesPath" ,module-path "-Stdio")))
51+
(with-current-buffer (find-file-noselect test-script)
52+
(should (eq major-mode 'powershell-mode))
53+
(should (apply #'eglot--connect (eglot--guess-contact)))
54+
(should (eglot-current-server))
55+
(let ((lsp (eglot-current-server)))
56+
(should (string= (eglot--project-nickname lsp) "PowerShellEditorServices"))
57+
(should (member (cons 'powershell-mode "powershell") (eglot--languages lsp))))
58+
(sleep-for 5) ; TODO: Wait for "textDocument/publishDiagnostics" instead
59+
(flymake-start)
60+
(goto-char (point-min))
61+
(flymake-goto-next-error)
62+
(should (eq 'flymake-warning (face-at-point))))))
63+
64+
(provide 'emacs-test)
65+
;;; emacs-test.el ends here

test/vim-simple-test.vim

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
let s:suite = themis#suite('pses')
2+
let s:assert = themis#helper('assert')
3+
4+
function s:suite.before()
5+
let l:pses_path = g:repo_root . '/module'
6+
let g:LanguageClient_serverCommands = {
7+
\ 'ps1': [ 'pwsh', '-NoLogo', '-NoProfile', '-Command',
8+
\ l:pses_path . '/PowerShellEditorServices/Start-EditorServices.ps1',
9+
\ '-BundledModulesPath', l:pses_path, '-Stdio' ]
10+
\ }
11+
let g:LanguageClient_serverStderr = 'DEBUG'
12+
let g:LanguageClient_loggingFile = g:repo_root . '/LanguageClient.log'
13+
let g:LanguageClient_serverStderr = g:repo_root . '/LanguageServer.log'
14+
endfunction
15+
16+
function s:suite.has_language_client()
17+
call s:assert.includes(&runtimepath, g:repo_root . '/LanguageClient-neovim')
18+
call s:assert.cmd_exists('LanguageClientStart')
19+
call s:assert.not_empty(g:LanguageClient_serverCommands)
20+
call s:assert.true(LanguageClient#HasCommand('ps1'))
21+
endfunction
22+
23+
function s:suite.analyzes_powershell_file()
24+
view test/vim-test.ps1 " This must not use quotes!
25+
26+
let l:bufnr = bufnr('vim-test.ps1$')
27+
call s:assert.not_equal(l:bufnr, -1)
28+
let l:bufinfo = getbufinfo(l:bufnr)[0]
29+
30+
call s:assert.equal(l:bufinfo.name, g:repo_root . '/test/vim-test.ps1')
31+
call s:assert.includes(getbufline(l:bufinfo.name, 1), 'function Do-Work {}')
32+
" TODO: This shouldn't be necessary, vim-ps1 works locally but not in CI.
33+
call setbufvar(l:bufinfo.bufnr, '&filetype', 'ps1')
34+
call s:assert.equal(getbufvar(l:bufinfo.bufnr, '&filetype'), 'ps1')
35+
36+
execute 'LanguageClientStart'
37+
execute 'sleep' 5
38+
call s:assert.equal(getbufvar(l:bufinfo.name, 'LanguageClient_isServerRunning'), 1)
39+
call s:assert.equal(getbufvar(l:bufinfo.name, 'LanguageClient_projectRoot'), g:repo_root)
40+
call s:assert.equal(getbufvar(l:bufinfo.name, 'LanguageClient_statusLineDiagnosticsCounts'), {'E': 0, 'W': 1, 'H': 0, 'I': 0})
41+
endfunction

test/vim-test.vim

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ let s:assert = themis#helper('assert')
44
function s:suite.before()
55
let l:pses_path = g:repo_root . '/module'
66
let g:LanguageClient_serverCommands = {
7-
\ 'ps1': ['pwsh', '-NoLogo', '-NoProfile', '-Command',
7+
\ 'ps1': [ 'pwsh', '-NoLogo', '-NoProfile', '-Command',
88
\ l:pses_path . '/PowerShellEditorServices/Start-EditorServices.ps1',
99
\ '-HostName', 'vim', '-HostProfileId', 'vim', '-HostVersion', '1.0.0',
1010
\ '-BundledModulesPath', l:pses_path, '-Stdio',

0 commit comments

Comments
 (0)