|
1 | 1 | import { expect } from "chai"
|
2 | 2 | import * as path from "path"
|
| 3 | +import * as os from "os" |
| 4 | +import * as fs from "fs" |
3 | 5 | import * as sinon from "sinon"
|
4 | 6 | import ApplyEditAdapter from "../../lib/adapters/apply-edit-adapter"
|
5 | 7 | import Convert from "../../lib/convert"
|
@@ -186,5 +188,235 @@ describe("ApplyEditAdapter", () => {
|
186 | 188 | expect(errorCalls.length).to.equal(1)
|
187 | 189 | expect(errorCalls[0].args[1].detail).to.equal(`Out of range edit on ${TEST_PATH4}:1:2`)
|
188 | 190 | })
|
| 191 | + |
| 192 | + it("handles rename resource operations", async () => { |
| 193 | + const directory = fs.mkdtempSync(os.tmpdir()) |
| 194 | + const oldUri = path.join(directory, "test.txt") |
| 195 | + const newUri = path.join(directory, "test-renamed.txt") |
| 196 | + fs.writeFileSync(oldUri, 'abcd') |
| 197 | + |
| 198 | + const result = await ApplyEditAdapter.onApplyEdit({ |
| 199 | + edit: { |
| 200 | + documentChanges: [ |
| 201 | + { |
| 202 | + kind: "rename", |
| 203 | + oldUri: oldUri, |
| 204 | + newUri: newUri, |
| 205 | + } |
| 206 | + ] |
| 207 | + }, |
| 208 | + }) |
| 209 | + |
| 210 | + expect(result.applied).to.equal(true) |
| 211 | + expect(fs.existsSync(newUri)).to.equal(true) |
| 212 | + expect(fs.readFileSync(newUri).toString()).to.equal('abcd') |
| 213 | + expect(fs.existsSync(oldUri)).to.equal(false) |
| 214 | + }) |
| 215 | + |
| 216 | + it("handles rename operation with ignoreIfExists option", async () => { |
| 217 | + const directory = fs.mkdtempSync(os.tmpdir()) |
| 218 | + const oldUri = path.join(directory, "test.txt") |
| 219 | + const newUri = path.join(directory, "test-renamed.txt") |
| 220 | + fs.writeFileSync(oldUri, 'abcd') |
| 221 | + fs.writeFileSync(newUri, 'efgh') |
| 222 | + |
| 223 | + const result = await ApplyEditAdapter.onApplyEdit({ |
| 224 | + edit: { |
| 225 | + documentChanges: [ |
| 226 | + { |
| 227 | + kind: "rename", |
| 228 | + oldUri: oldUri, |
| 229 | + newUri: newUri, |
| 230 | + options: { |
| 231 | + ignoreIfExists: true |
| 232 | + } |
| 233 | + } |
| 234 | + ] |
| 235 | + }, |
| 236 | + }) |
| 237 | + |
| 238 | + expect(result.applied).to.equal(true) |
| 239 | + expect(fs.existsSync(oldUri)).to.equal(true) |
| 240 | + expect(fs.readFileSync(newUri).toString()).to.equal('efgh') |
| 241 | + }) |
| 242 | + |
| 243 | + it("handles rename operation with overwrite option", async () => { |
| 244 | + const directory = fs.mkdtempSync(os.tmpdir()) |
| 245 | + const oldUri = path.join(directory, "test.txt") |
| 246 | + const newUri = path.join(directory, "test-renamed.txt") |
| 247 | + fs.writeFileSync(oldUri, 'abcd') |
| 248 | + fs.writeFileSync(newUri, 'efgh') |
| 249 | + |
| 250 | + const result = await ApplyEditAdapter.onApplyEdit({ |
| 251 | + edit: { |
| 252 | + documentChanges: [ |
| 253 | + { |
| 254 | + kind: "rename", |
| 255 | + oldUri: oldUri, |
| 256 | + newUri: newUri, |
| 257 | + options: { |
| 258 | + overwrite: true, |
| 259 | + ignoreIfExists: true // Overwrite wins over ignoreIfExists |
| 260 | + } |
| 261 | + } |
| 262 | + ] |
| 263 | + }, |
| 264 | + }) |
| 265 | + |
| 266 | + expect(result.applied).to.equal(true) |
| 267 | + expect(fs.existsSync(oldUri)).to.equal(false) |
| 268 | + expect(fs.readFileSync(newUri).toString()).to.equal('abcd') |
| 269 | + }) |
| 270 | + |
| 271 | + it("throws an error on rename operation if target exists", async () => { |
| 272 | + const directory = fs.mkdtempSync(os.tmpdir()) |
| 273 | + const oldUri = path.join(directory, "test.txt") |
| 274 | + const newUri = path.join(directory, "test-renamed.txt") |
| 275 | + fs.writeFileSync(oldUri, 'abcd') |
| 276 | + fs.writeFileSync(newUri, 'efgh') |
| 277 | + |
| 278 | + const result = await ApplyEditAdapter.onApplyEdit({ |
| 279 | + edit: { |
| 280 | + documentChanges: [ |
| 281 | + { |
| 282 | + kind: "rename", |
| 283 | + oldUri: oldUri, |
| 284 | + newUri: newUri, |
| 285 | + } |
| 286 | + ] |
| 287 | + }, |
| 288 | + }) |
| 289 | + |
| 290 | + expect(result.applied).to.equal(false) |
| 291 | + expect(fs.existsSync(oldUri)).to.equal(true) |
| 292 | + expect(fs.readFileSync(oldUri).toString()).to.equal('abcd') |
| 293 | + expect(fs.existsSync(newUri)).to.equal(true) |
| 294 | + expect(fs.readFileSync(newUri).toString()).to.equal('efgh') |
| 295 | + |
| 296 | + expect( |
| 297 | + (atom as any).notifications.addError.calledWith("workspace/applyEdits failed", { |
| 298 | + description: "Failed to apply edits.", |
| 299 | + detail: "Error during rename resource operation: Target exists.", |
| 300 | + }) |
| 301 | + ).to.equal(true) |
| 302 | + }) |
| 303 | + |
| 304 | + it("handles delete resource operations on files", async () => { |
| 305 | + const directory = fs.mkdtempSync(os.tmpdir()) |
| 306 | + const uri = path.join(directory, "test.txt") |
| 307 | + fs.writeFileSync(uri, 'abcd') |
| 308 | + |
| 309 | + const result = await ApplyEditAdapter.onApplyEdit({ |
| 310 | + edit: { |
| 311 | + documentChanges: [ |
| 312 | + { |
| 313 | + kind: "delete", |
| 314 | + uri: uri |
| 315 | + } |
| 316 | + ] |
| 317 | + }, |
| 318 | + }) |
| 319 | + |
| 320 | + expect(result.applied).to.equal(true) |
| 321 | + expect(fs.existsSync(uri)).to.equal(false) |
| 322 | + }) |
| 323 | + |
| 324 | + it("handles delete resource operations on directories", async () => { |
| 325 | + const directory = fs.mkdtempSync(os.tmpdir()) |
| 326 | + const file1 = path.join(directory, '1.txt') |
| 327 | + const file2 = path.join(directory, '2.txt') |
| 328 | + fs.writeFileSync(file1, '1') |
| 329 | + fs.writeFileSync(file2, '2') |
| 330 | + |
| 331 | + const result = await ApplyEditAdapter.onApplyEdit({ |
| 332 | + edit: { |
| 333 | + documentChanges: [ |
| 334 | + { |
| 335 | + kind: "delete", |
| 336 | + uri: directory, |
| 337 | + options: { |
| 338 | + recursive: true |
| 339 | + } |
| 340 | + } |
| 341 | + ] |
| 342 | + }, |
| 343 | + }) |
| 344 | + |
| 345 | + expect(result.applied).to.equal(true) |
| 346 | + expect(fs.existsSync(directory)).to.equal(false) |
| 347 | + expect(fs.existsSync(file1)).to.equal(false) |
| 348 | + expect(fs.existsSync(file2)).to.equal(false) |
| 349 | + }) |
| 350 | + |
| 351 | + it("throws an error when deleting a non-empty directory without recursive option", async () => { |
| 352 | + const directory = fs.mkdtempSync(os.tmpdir()) |
| 353 | + const file1 = path.join(directory, '1.txt') |
| 354 | + const file2 = path.join(directory, '2.txt') |
| 355 | + fs.writeFileSync(file1, '1') |
| 356 | + fs.writeFileSync(file2, '2') |
| 357 | + |
| 358 | + const result = await ApplyEditAdapter.onApplyEdit({ |
| 359 | + edit: { |
| 360 | + documentChanges: [ |
| 361 | + { |
| 362 | + kind: "delete", |
| 363 | + uri: directory, |
| 364 | + options: { |
| 365 | + recursive: false |
| 366 | + } |
| 367 | + } |
| 368 | + ] |
| 369 | + }, |
| 370 | + }) |
| 371 | + |
| 372 | + expect(result.applied).to.equal(false) |
| 373 | + expect(fs.existsSync(directory)).to.equal(true) |
| 374 | + expect(fs.existsSync(file1)).to.equal(true) |
| 375 | + expect(fs.existsSync(file2)).to.equal(true) |
| 376 | + const errorCalls = (atom as any).notifications.addError.getCalls() |
| 377 | + expect(errorCalls.length).to.equal(1) |
| 378 | + expect(errorCalls[0].args[1].detail).to.match(/Error during delete resource operation: (.*)/) |
| 379 | + }) |
| 380 | + |
| 381 | + |
| 382 | + it("throws an error on delete operation if target doesnt exist", async () => { |
| 383 | + const result = await ApplyEditAdapter.onApplyEdit({ |
| 384 | + edit: { |
| 385 | + documentChanges: [ |
| 386 | + { |
| 387 | + kind: "delete", |
| 388 | + uri: path.join(os.tmpdir(), "unexisting.txt"), |
| 389 | + } |
| 390 | + ] |
| 391 | + }, |
| 392 | + }) |
| 393 | + // |
| 394 | + expect(result.applied).to.equal(false) |
| 395 | + expect( |
| 396 | + (atom as any).notifications.addError.calledWith("workspace/applyEdits failed", { |
| 397 | + description: "Failed to apply edits.", |
| 398 | + detail: "Error during delete resource operation: Target doesn't exist.", |
| 399 | + }) |
| 400 | + ).to.equal(true) |
| 401 | + }) |
| 402 | + |
| 403 | + it("handles create resource operations", async () => { |
| 404 | + const directory = fs.mkdtempSync(os.tmpdir()) |
| 405 | + const uri = path.join(directory, "test.txt") |
| 406 | + |
| 407 | + const result = await ApplyEditAdapter.onApplyEdit({ |
| 408 | + edit: { |
| 409 | + documentChanges: [ |
| 410 | + { |
| 411 | + kind: "create", |
| 412 | + uri: uri |
| 413 | + } |
| 414 | + ] |
| 415 | + }, |
| 416 | + }) |
| 417 | + |
| 418 | + expect(result.applied).to.equal(true) |
| 419 | + expect(fs.existsSync(uri)).to.equal(true) |
| 420 | + }) |
189 | 421 | })
|
190 | 422 | })
|
0 commit comments