• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

streetsidesoftware / cspell / 12339180630

15 Dec 2024 01:23PM UTC coverage: 94.057%. First build
12339180630

Pull #6671

github

web-flow
Merge 825fb3d46 into 2ed706e50
Pull Request #6671: feat: Support Windows UNC files.

14048 of 16216 branches covered (86.63%)

44 of 59 new or added lines in 13 files covered. (74.58%)

15147 of 16104 relevant lines covered (94.06%)

31160.14 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

52.38
/packages/cspell-url/src/fileUrl.mts
1
import { fileURLToPath, pathToFileURL } from 'node:url';
2

3
import { hasProtocol } from './url.mjs';
4

5
export const isWindows = process.platform === 'win32';
3✔
6

7
const windowsUrlPathRegExp = /^\/[a-zA-Z]:\//;
3✔
8

9
export function isWindowsPathnameWithDriveLatter(pathname: string): boolean {
NEW
10
    return windowsUrlPathRegExp.test(pathname);
×
11
}
12

13
/**
14
 * @param url - URL or string to check if it is a file URL.
15
 * @returns true if the URL is a file URL.
16
 */
17
export function isFileURL(url: URL | string): boolean {
18
    return hasProtocol(url, 'file:');
14✔
19
}
20

21
/**
22
 * Convert a URL into a string. If it is a file URL, convert it to a path.
23
 * @param url - URL
24
 * @returns path or href
25
 */
26
export function toFilePathOrHref(url: URL | string): string {
27
    return isFileURL(url) && url.toString().startsWith('file:///') ? toFilePath(url) : url.toString();
8✔
28
}
29

30
function toFilePath(url: string | URL): string {
31
    try {
7✔
32
        // Fix drive letter if necessary.
33
        if (isWindows) {
7!
NEW
34
            const u = new URL(url);
×
NEW
35
            if (!isWindowsPathnameWithDriveLatter(u.pathname)) {
×
NEW
36
                const cwdUrl = pathToFileURL(process.cwd());
×
NEW
37
                if (cwdUrl.hostname) {
×
NEW
38
                    return fileURLToPath(new URL(u.pathname, cwdUrl));
×
39
                }
NEW
40
                const drive = cwdUrl.pathname.split('/')[1];
×
NEW
41
                u.pathname = `/${drive}${u.pathname}`;
×
NEW
42
                return fileURLToPath(u);
×
43
            }
44
        }
45
        return pathWindowsDriveLetterToUpper(fileURLToPath(url));
7✔
46
    } catch {
47
        // console.error('Failed to convert URL to path', url);
NEW
48
        return url.toString();
×
49
    }
50
}
51

52
export const regExpWindowsPathDriveLetter = /^([a-zA-Z]):[\\/]/;
3✔
53

54
export function pathWindowsDriveLetterToUpper(absoluteFilePath: string): string {
55
    return absoluteFilePath.replace(regExpWindowsPathDriveLetter, (s) => s.toUpperCase());
21✔
56
}
57

58
const regExpWindowsFileUrl = /^file:\/\/\/[a-zA-Z]:\//;
3✔
59

60
/**
61
 * Test if a url is a file url with a windows path. It does check for UNC paths.
62
 * @param url - the url
63
 * @returns true if the url is a file url with a windows path with a drive letter.
64
 */
65
export function isWindowsFileUrl(url: URL | string): boolean {
66
    return regExpWindowsFileUrl.test(url.toString());
12✔
67
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc