1
+ import { TextEditor } from "atom"
1
2
import AutoLanguageClient from "../lib/auto-languageclient"
2
- import { projectPathToWorkspaceFolder , ServerManager } from "../lib/server-manager"
3
+ import {
4
+ projectPathToWorkspaceFolder ,
5
+ ServerManager ,
6
+ ActiveServer ,
7
+ considerAdditionalPath ,
8
+ normalizePath ,
9
+ } from "../lib/server-manager"
3
10
import { FakeAutoLanguageClient } from "./helpers"
4
- import { dirname } from "path"
11
+ import { dirname , join } from "path"
5
12
6
13
function mockEditor ( uri : string , scopeName : string ) : any {
7
14
return {
@@ -12,20 +19,84 @@ function mockEditor(uri: string, scopeName: string): any {
12
19
}
13
20
}
14
21
22
+ function setupClient ( ) {
23
+ atom . workspace . getTextEditors ( ) . forEach ( ( editor ) => editor . destroy ( ) )
24
+ atom . project . getPaths ( ) . forEach ( ( project ) => atom . project . removePath ( project ) )
25
+ const client = new FakeAutoLanguageClient ( )
26
+ client . activate ( )
27
+ return client
28
+ }
29
+
30
+ function setupServerManager ( client = setupClient ( ) ) {
31
+ /* eslint-disable-next-line dot-notation */
32
+ const serverManager = client [ "_serverManager" ]
33
+ return serverManager
34
+ }
35
+
15
36
describe ( "AutoLanguageClient" , ( ) => {
37
+ describe ( "determineProjectPath" , ( ) => {
38
+ it ( "returns the project path for an internal or an external file in the project" , async ( ) => {
39
+ if ( process . platform === "darwin" ) {
40
+ // there is nothing OS specific about the code. It just hits the limits that MacOS can handle in this test
41
+ pending ( "skipped on MacOS" )
42
+ return
43
+ }
44
+ const client = setupClient ( )
45
+ const serverManager = setupServerManager ( client )
46
+
47
+ // "returns null when a single file is open"
48
+
49
+ let textEditor = ( await atom . workspace . open ( __filename ) ) as TextEditor
50
+ /* eslint-disable-next-line dot-notation */
51
+ expect ( client [ "determineProjectPath" ] ( textEditor ) ) . toBeNull ( )
52
+ textEditor . destroy ( )
53
+
54
+ // "returns the project path when a file of that project is open"
55
+ const projectPath = __dirname
56
+
57
+ // gives the open workspace folder
58
+ atom . project . addPath ( projectPath )
59
+ await serverManager . startServer ( projectPath )
60
+
61
+ textEditor = ( await atom . workspace . open ( __filename ) ) as TextEditor
62
+ /* eslint-disable-next-line dot-notation */
63
+ expect ( client [ "determineProjectPath" ] ( textEditor ) ) . toBe ( normalizePath ( projectPath ) )
64
+ textEditor . destroy ( )
65
+
66
+ // "returns the project path when an external file is open and it is not in additional paths"
67
+
68
+ const externalDir = join ( dirname ( projectPath ) , "lib" )
69
+ const externalFile = join ( externalDir , "main.js" )
70
+
71
+ // gives the open workspace folder
72
+ atom . project . addPath ( projectPath )
73
+ await serverManager . startServer ( projectPath )
74
+
75
+ textEditor = ( await atom . workspace . open ( externalFile ) ) as TextEditor
76
+ /* eslint-disable-next-line dot-notation */
77
+ expect ( client [ "determineProjectPath" ] ( textEditor ) ) . toBeNull ( )
78
+ textEditor . destroy ( )
79
+
80
+ // "returns the project path when an external file is open and it is in additional paths"
81
+
82
+ // get server
83
+ const server = serverManager . getActiveServers ( ) [ 0 ]
84
+ expect ( typeof server . additionalPaths ) . toBe ( "object" ) // Set()
85
+ // add additional path
86
+ considerAdditionalPath ( server as ActiveServer & { additionalPaths : Set < string > } , externalDir )
87
+ expect ( server . additionalPaths ?. has ( externalDir ) ) . toBeTrue ( )
88
+
89
+ textEditor = ( await atom . workspace . open ( externalFile ) ) as TextEditor
90
+ /* eslint-disable-next-line dot-notation */
91
+ expect ( client [ "determineProjectPath" ] ( textEditor ) ) . toBe ( normalizePath ( projectPath ) )
92
+ textEditor . destroy ( )
93
+ } )
94
+ } )
16
95
describe ( "ServerManager" , ( ) => {
17
96
describe ( "WorkspaceFolders" , ( ) => {
18
- let client : FakeAutoLanguageClient
19
97
let serverManager : ServerManager
20
-
21
98
beforeEach ( ( ) => {
22
- atom . workspace . getTextEditors ( ) . forEach ( ( editor ) => editor . destroy ( ) )
23
- atom . project . getPaths ( ) . forEach ( ( project ) => atom . project . removePath ( project ) )
24
- client = new FakeAutoLanguageClient ( )
25
- client . activate ( )
26
-
27
- /* eslint-disable-next-line dot-notation */
28
- serverManager = client [ "_serverManager" ]
99
+ serverManager = setupServerManager ( )
29
100
} )
30
101
31
102
afterEach ( ( ) => {
0 commit comments