blob: b75dc29c48278ed08d63569df5fe52a62d536215 [file] [log] [blame]
Bram Moolenaarf38c86e2017-11-28 14:19:07 +01001" Test for URLs in help documents.
2"
3" Opens a new window with all found URLS followed by return code from curl
4" (anything other than 0 means unreachable)
5"
6" Written by Christian Brabandt.
7
8func Test_check_URLs()
Restorerb23c1fc2023-11-04 08:57:09 +00009"20.10.23, added by Restorer
Bram Moolenaarf38c86e2017-11-28 14:19:07 +010010 if has("win32")
Restorerb23c1fc2023-11-04 08:57:09 +000011 let s:outdev = 'nul'
12 else
13 let s:outdev = '/dev/null'
Bram Moolenaarf38c86e2017-11-28 14:19:07 +010014 endif
Christian Brabandt1c5728e2024-05-11 11:12:40 +020015" Restorer: For Windows users. If "curl" or "wget" is installed on the system
16" but not in %PATH%, add the full path to them to %PATH% environment variable.
Bram Moolenaarf38c86e2017-11-28 14:19:07 +010017 if executable('curl')
18 " Note: does not follow redirects!
Christian Brabandt1c5728e2024-05-11 11:12:40 +020019 let s:command1 = 'curl --silent --max-time 5 --fail --output ' ..s:outdev.. ' --head '
Restorerb23c1fc2023-11-04 08:57:09 +000020 let s:command2 = ""
Bram Moolenaarf38c86e2017-11-28 14:19:07 +010021 elseif executable('wget')
22 " Note: only allow a couple of redirects
Restorerb23c1fc2023-11-04 08:57:09 +000023 let s:command1 = 'wget --quiet -S --spider --max-redirect=2 --timeout=5 --tries=2 -O ' ..s:outdev.. ' '
24 let s:command2 = ""
25 elseif has("win32") "20.10.23, added by Restorer
26 if executable('powershell')
27 if 2 == system('powershell -nologo -noprofile "$psversiontable.psversion.major"')
28 echoerr 'To work in OS Windows requires the program "PowerShell" version 3.0 or higher'
29 return
30 endif
31 let s:command1 =
32 \ "powershell -nologo -noprofile \"{[Net.ServicePointManager]::SecurityProtocol = 'Tls12, Tls11, Tls, Ssl3'};try{(Invoke-WebRequest -MaximumRedirection 2 -TimeoutSec 5 -Uri "
33 let s:command2 = ').StatusCode}catch{exit [int]$Error[0].Exception.Status}"'
34 endif
Bram Moolenaarf38c86e2017-11-28 14:19:07 +010035 else
Restorerb23c1fc2023-11-04 08:57:09 +000036 echoerr 'Only works when "curl" or "wget", or "powershell" is available'
Bram Moolenaarf38c86e2017-11-28 14:19:07 +010037 return
38 endif
39
Restorerb23c1fc2023-11-04 08:57:09 +000040 " Do the testing.
41 set report =999
42 set nomore shm +=s
43
Bram Moolenaarf38c86e2017-11-28 14:19:07 +010044 let pat='\(https\?\|ftp\)://[^\t* ]\+'
45 exe 'helpgrep' pat
46 helpclose
47
48 let urls = map(getqflist(), 'v:val.text')
49 " do not use submatch(1)!
50 let urls = map(urls, {key, val -> matchstr(val, pat)})
51 " remove examples like user@host (invalid urls)
52 let urls = filter(urls, 'v:val !~ "@"')
53 " Remove example URLs which are invalid
54 let urls = filter(urls, {key, val -> val !~ '\<\(\(my\|some\)\?host\|machine\|hostname\|file\)\>'})
55 new
56 put =urls
57 " remove some more invalid items
58 " empty lines
Restorerb23c1fc2023-11-04 08:57:09 +000059 "20.10.23, Restorer: '_' is a little faster, see `:h global`
60 v/./d _
Bram Moolenaarf38c86e2017-11-28 14:19:07 +010061 " remove # anchors
62 %s/#.*$//e
63 " remove trailing stuff (parenthesis, dot, comma, quotes), but only for HTTP
64 " links
Restorerb23c1fc2023-11-04 08:57:09 +000065 g/^h/s#[.),'"`/>][:.,]\?$##
66 g#^[hf]t\?tp:/\(/\?\.*\)$#d _
67 silent! g/ftp://,$/d _
68 silent! g/=$/d _
Bram Moolenaarf38c86e2017-11-28 14:19:07 +010069 let a = getline(1,'$')
70 let a = uniq(sort(a))
Restorerb23c1fc2023-11-04 08:57:09 +000071 %d _
Bram Moolenaarf38c86e2017-11-28 14:19:07 +010072 call setline(1, a)
73
Bram Moolenaarf38c86e2017-11-28 14:19:07 +010074 %s/.*/\=TestURL(submatch(0))/
75
76 " highlight the failures
77 /.* \([0-9]*[1-9]\|[0-9]\{2,}\)$
78endfunc
79
80func TestURL(url)
81 " Relies on the return code to determine whether a page is valid
82 echom printf("Testing URL: %d/%d %s", line('.'), line('$'), a:url)
Restorerb23c1fc2023-11-04 08:57:09 +000083 call system(s:command1 .. shellescape(a:url) .. s:command2)
Bram Moolenaarf38c86e2017-11-28 14:19:07 +010084 return printf("%s %d", a:url, v:shell_error)
85endfunc
86
87call Test_check_URLs()
Restorerb23c1fc2023-11-04 08:57:09 +000088
89" vim: sw=2 sts=2 et