|
| 1 | +""" |
| 2 | +The goal of these tests is to check that ALS remembers a base configuration to which it |
| 3 | +can revert settings when receiving null values in didChangeConfiguration notifiactions. |
| 4 | +
|
| 5 | +It's important to check that the base configuration can be obtained both when there are |
| 6 | +config files (.als.json or User config file), and when there aren't - i.e. when settings |
| 7 | +are sent with the initialize request, or with the first didChangeConfiguration |
| 8 | +notification. |
| 9 | +
|
| 10 | +To observe the active configuration, we use a project file where the object directory is |
| 11 | +defined via an external scenario variable and we use the als-object-dir command to query |
| 12 | +that. |
| 13 | +
|
| 14 | +We also need a way to wait until the project has finished loading in order to query its |
| 15 | +object dir. To do that we add at least one source file and await the end of indexing. |
| 16 | +
|
| 17 | +The scenarios we want to test: |
| 18 | +
|
| 19 | +1. There is a config file. Then the initialize request also provides settings, but the |
| 20 | +base config should be the config file. |
| 21 | +
|
| 22 | +2. There are no config files, and the initialize request contains settings with non-null |
| 23 | +values that should be the base config. |
| 24 | +
|
| 25 | +3. There are no config files, and the initialize request contains settings with all null |
| 26 | +values. This is the case when no ada.* settings are set in VS Code. The config at |
| 27 | +initialize should be considered the base config. |
| 28 | +
|
| 29 | +4. There are no config files. The initialize request doesn't contain any settings. The |
| 30 | +first didChangeConfig notification should be considered as the base config. |
| 31 | +""" |
| 32 | + |
| 33 | +import os |
| 34 | +from lsprotocol.types import ClientCapabilities, InitializeParams |
| 35 | +from drivers.pylsp import ( |
| 36 | + URI, |
| 37 | + ALSClientServerConfig, |
| 38 | + ALSLanguageClient, |
| 39 | + ALSSettings, |
| 40 | + assertEqual, |
| 41 | + awaitIndexingEnd, |
| 42 | + test, |
| 43 | +) |
| 44 | + |
| 45 | + |
| 46 | +@test( |
| 47 | + config=ALSClientServerConfig( |
| 48 | + server_command=[ |
| 49 | + os.environ.get("ALS", "ada_language_server"), |
| 50 | + "--config", |
| 51 | + "my_config.json", |
| 52 | + ], |
| 53 | + ), |
| 54 | + initialize=False, |
| 55 | +) |
| 56 | +async def test1(lsp: ALSLanguageClient) -> None: |
| 57 | + # Even if we send settings in the initialize request, the config file should |
| 58 | + # constitue the base config. |
| 59 | + settings: ALSSettings = { |
| 60 | + "scenarioVariables": {"Var": "value-from-init"}, |
| 61 | + } |
| 62 | + await lsp.initialize_session( |
| 63 | + InitializeParams( |
| 64 | + ClientCapabilities(), |
| 65 | + root_uri=URI(os.getcwd()), |
| 66 | + initialization_options={"ada": settings}, |
| 67 | + ) |
| 68 | + ) |
| 69 | + # Because no project file was set, we need a didOpen to load the project |
| 70 | + lsp.didOpenVirtual() |
| 71 | + await awaitIndexingEnd(lsp) |
| 72 | + assertEqual(await lsp.getObjDirBasename(), "value-from-init") |
| 73 | + |
| 74 | + # Now let's change the settings |
| 75 | + lsp.didChangeConfig({"scenarioVariables": {"Var": "new-value"}}) |
| 76 | + await awaitIndexingEnd(lsp) |
| 77 | + assertEqual(await lsp.getObjDirBasename(), "new-value") |
| 78 | + |
| 79 | + # Now we send a null value to revert to the base config which should be the config |
| 80 | + # file, not the initialize request. |
| 81 | + lsp.didChangeConfig({"scenarioVariables": None}) |
| 82 | + await awaitIndexingEnd(lsp) |
| 83 | + assertEqual(await lsp.getObjDirBasename(), "value-from-config-file") |
| 84 | + |
| 85 | + |
| 86 | +@test(initialize=False) |
| 87 | +async def test2(lsp: ALSLanguageClient) -> None: |
| 88 | + # In this scenario there are no config files. The settings in the initialize request |
| 89 | + # should constitute the base config. |
| 90 | + settings: ALSSettings = { |
| 91 | + "scenarioVariables": {"Var": "value-from-init"}, |
| 92 | + } |
| 93 | + await lsp.initialize_session( |
| 94 | + InitializeParams( |
| 95 | + ClientCapabilities(), |
| 96 | + root_uri=URI(os.getcwd()), |
| 97 | + initialization_options={"ada": settings}, |
| 98 | + ) |
| 99 | + ) |
| 100 | + # Because no project file was set, we need a didOpen to load the project |
| 101 | + lsp.didOpenVirtual() |
| 102 | + await awaitIndexingEnd(lsp) |
| 103 | + assertEqual(await lsp.getObjDirBasename(), "value-from-init") |
| 104 | + |
| 105 | + # Now let's change the settings and revert back to see if we revert to the right |
| 106 | + # value. |
| 107 | + lsp.didChangeConfig({"scenarioVariables": {"Var": "new-value"}}) |
| 108 | + await awaitIndexingEnd(lsp) |
| 109 | + assertEqual(await lsp.getObjDirBasename(), "new-value") |
| 110 | + |
| 111 | + lsp.didChangeConfig({"scenarioVariables": None}) |
| 112 | + await awaitIndexingEnd(lsp) |
| 113 | + assertEqual(await lsp.getObjDirBasename(), "value-from-init") |
| 114 | + |
| 115 | + |
| 116 | +@test(initialize=False) |
| 117 | +async def test3(lsp: ALSLanguageClient) -> None: |
| 118 | + # This scenario reproduces what happens in VS Code when there are no ada.* settings |
| 119 | + # |
| 120 | + # The settings in the initialize request are null |
| 121 | + settings: ALSSettings = { |
| 122 | + "scenarioVariables": None, |
| 123 | + } |
| 124 | + await lsp.initialize_session( |
| 125 | + InitializeParams( |
| 126 | + ClientCapabilities(), |
| 127 | + root_uri=URI(os.getcwd()), |
| 128 | + initialization_options={"ada": settings}, |
| 129 | + ) |
| 130 | + ) |
| 131 | + # Because no project file was set, we need a didOpen to load the project |
| 132 | + lsp.didOpenVirtual() |
| 133 | + await awaitIndexingEnd(lsp) |
| 134 | + # No value was provided for the scenario variable, so we should get the default |
| 135 | + # value defined in the project. |
| 136 | + assertEqual(await lsp.getObjDirBasename(), "value-from-prj") |
| 137 | + |
| 138 | + # Now let's change the settings and revert back to see if we revert to the right |
| 139 | + # value. |
| 140 | + lsp.didChangeConfig({"scenarioVariables": {"Var": "new-value"}}) |
| 141 | + await awaitIndexingEnd(lsp) |
| 142 | + assertEqual(await lsp.getObjDirBasename(), "new-value") |
| 143 | + |
| 144 | + lsp.didChangeConfig({"scenarioVariables": None}) |
| 145 | + await awaitIndexingEnd(lsp) |
| 146 | + assertEqual(await lsp.getObjDirBasename(), "value-from-prj") |
| 147 | + |
| 148 | + |
| 149 | +@test(initialize=False) |
| 150 | +async def test4(lsp: ALSLanguageClient) -> None: |
| 151 | + # There is no config file |
| 152 | + await lsp.initialize_session( |
| 153 | + InitializeParams( |
| 154 | + ClientCapabilities(), |
| 155 | + root_uri=URI(os.getcwd()), |
| 156 | + # initialize has no settings |
| 157 | + ) |
| 158 | + ) |
| 159 | + # Because no project file was set, we need a didOpen to load the project |
| 160 | + lsp.didOpenVirtual() |
| 161 | + await awaitIndexingEnd(lsp) |
| 162 | + # No value was provided for the scenario variable, so we should get the default |
| 163 | + # value defined in the project. |
| 164 | + assertEqual(await lsp.getObjDirBasename(), "value-from-prj") |
| 165 | + |
| 166 | + # Now let's send the first didChangeConfiguration. This should constitute the base |
| 167 | + # config. |
| 168 | + lsp.didChangeConfig( |
| 169 | + {"scenarioVariables": {"Var": "value-from-first-config-change"}} |
| 170 | + ) |
| 171 | + await awaitIndexingEnd(lsp) |
| 172 | + assertEqual(await lsp.getObjDirBasename(), "value-from-first-config-change") |
| 173 | + |
| 174 | + # Now we change to another value, and revert with a null value. |
| 175 | + lsp.didChangeConfig({"scenarioVariables": {"Var": "new-value"}}) |
| 176 | + await awaitIndexingEnd(lsp) |
| 177 | + assertEqual(await lsp.getObjDirBasename(), "new-value") |
| 178 | + |
| 179 | + lsp.didChangeConfig({"scenarioVariables": None}) |
| 180 | + await awaitIndexingEnd(lsp) |
| 181 | + assertEqual(await lsp.getObjDirBasename(), "value-from-first-config-change") |
0 commit comments