File: Utils.swift

package info (click to toggle)
swiftlang 6.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,856,264 kB
  • sloc: cpp: 9,995,718; ansic: 2,234,019; asm: 1,092,167; python: 313,940; objc: 82,726; f90: 80,126; lisp: 38,373; pascal: 25,580; sh: 20,378; ml: 5,058; perl: 4,751; makefile: 4,725; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (126 lines) | stat: -rw-r--r-- 4,286 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

package import Foundation
package import LanguageServerProtocol
import SwiftExtensions

import struct TSCBasic.AbsolutePath

extension Language {
  var fileExtension: String {
    switch self {
    case .objective_c: return "m"
    case .markdown: return "md"
    case .tutorial: return "tutorial"
    default: return self.rawValue
    }
  }

  init?(fileExtension: String) {
    switch fileExtension {
    case "c": self = .c
    case "cpp": self = .cpp
    case "m": self = .objective_c
    case "mm": self = .objective_cpp
    case "swift": self = .swift
    case "md": self = .markdown
    case "tutorial": self = .tutorial
    default: return nil
    }
  }
}

extension DocumentURI {
  /// Construct a `DocumentURI` by creating a unique URI for a document of the given language.
  package init(for language: Language, testName: String = #function) {
    let testBaseName = testName.prefix(while: \.isLetter)

    #if os(Windows)
    let url = URL(fileURLWithPath: "C:/\(testBaseName)/\(UUID())/test.\(language.fileExtension)")
    #else
    let url = URL(fileURLWithPath: "/\(testBaseName)/\(UUID())/test.\(language.fileExtension)")
    #endif

    self.init(url)
  }
}

package let cleanScratchDirectories =
  (ProcessInfo.processInfo.environment["SOURCEKIT_LSP_KEEP_TEST_SCRATCH_DIR"] == nil)

package func testScratchName(testName: String = #function) -> String {
  var uuid = UUID().uuidString[...]
  if let firstDash = uuid.firstIndex(of: "-") {
    uuid = uuid[..<firstDash]
  }

  // Including the test name in the directory frequently makes path lengths of test files exceed the maximum path length
  // on Windows. Choose shorter directory names on that platform to avoid that issue.
  #if os(Windows)
  return String(uuid)
  #else
  let testBaseName = testName.prefix(while: \.isLetter)
  return "\(testBaseName)-\(uuid)"
  #endif
}

/// An empty directory in which a test with `#function` name `testName` can store temporary data.
package func testScratchDir(testName: String = #function) throws -> URL {
  #if os(Windows)
  // Use a shorter test scratch dir name on Windows to not exceed MAX_PATH length
  let testScratchDirsName = "lsp-test"
  #else
  let testScratchDirsName = "sourcekit-lsp-test-scratch"
  #endif

  let url = try FileManager.default.temporaryDirectory.realpath
    .appendingPathComponent(testScratchDirsName)
    .appendingPathComponent(testScratchName(testName: testName), isDirectory: true)

  try? FileManager.default.removeItem(at: url)
  try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)
  return url
}

/// Execute `body` with a path to a temporary scratch directory for the given
/// test name.
///
/// The temporary directory will be deleted at the end of `directory` unless the
/// `SOURCEKIT_LSP_KEEP_TEST_SCRATCH_DIR` environment variable is set.
package func withTestScratchDir<T>(
  @_inheritActorContext _ body: @Sendable (URL) async throws -> T,
  testName: String = #function
) async throws -> T {
  let scratchDirectory = try testScratchDir(testName: testName)
  try FileManager.default.createDirectory(at: scratchDirectory, withIntermediateDirectories: true)
  defer {
    if cleanScratchDirectories {
      try? FileManager.default.removeItem(at: scratchDirectory)
    }
  }
  return try await body(scratchDirectory)
}

var globalModuleCache: URL? {
  get throws {
    if let customModuleCache = ProcessInfo.processInfo.environment["SOURCEKIT_LSP_TEST_MODULE_CACHE"] {
      if customModuleCache.isEmpty {
        return nil
      }
      return URL(fileURLWithPath: customModuleCache)
    }
    return try FileManager.default.temporaryDirectory.realpath
      .appendingPathComponent("sourcekit-lsp-test-scratch")
      .appendingPathComponent("shared-module-cache")
  }
}