Debian Codegraph @

inst/testme/test-utils,cluster.R

raw
1 message("*** utils,cluster ...")
2
3 shQuote <- parallelly:::shQuote
4
5 check_types <- function(cmd = "foo bar", os = NULL) {
6 if (is.null(os)) {
7 info <- ""
8 } else {
9 environment(shQuote)[[".Platform"]] <- list(OS.type = os)
10 on.exit(rm(list = ".Platform", envir = environment(shQuote)))
11 info <- sprintf(" with os = '%s'", os)
12 }
13
14 for (type in list("sh", "cmd", "none", NULL, NA)) {
15 type_str <- if (is.null(type)) "NULL" else sprintf('"%s"', type)
16 message(sprintf("- sQuote(... type = %s)%s", type_str, info))
17 if (is.null(type)) {
18 value <- shQuote(cmd, type = type)
19 if (is.null(os) || os == .Platform[["OS.type"]]) {
20 truth <- base::shQuote(cmd)
21 } else if (os == "unix") {
22 truth <- base::shQuote(cmd, type = "sh")
23 } else if (os == "windows") {
24 truth <- base::shQuote(cmd, type = "cmd")
25 }
26 } else if (is.na(type)) {
27 value <- shQuote(cmd)
28 if (is.null(os) || os == .Platform[["OS.type"]]) {
29 truth <- base::shQuote(cmd)
30 } else if (os == "unix") {
31 truth <- base::shQuote(cmd, type = "sh")
32 } else if (os == "windows") {
33 truth <- base::shQuote(cmd, type = "cmd")
34 }
35 } else if (type == "none") {
36 value <- shQuote(cmd, type = type)
37 truth <- cmd
38 } else {
39 value <- shQuote(cmd, type = type)
40 truth <- base::shQuote(cmd, type = type)
41 }
42 str(list(value = value, truth = truth))
43 stopifnot(value == truth)
44 }
45 }
46
47
48 message("- sQuote()")
49
50 cmd <- "foo bar"
51 stopifnot(shQuote(cmd) == base::shQuote(cmd))
52
53
54 for (os in list(NULL, "unix", "windows")) {
55 check_types(cmd = cmd, os = os)
56 }
57
58
59 message("- is_localhost()")
60
61 is_localhost <- parallelly:::is_localhost
62 ## Reset cache
63 is_localhost(worker = NULL, hostname = NULL)
64
65 ## Known localhost names
66 stopifnot(
67 is_localhost("localhost"),
68 is_localhost("127.0.0.1")
69 )
70
71 ## Current hostname is localhost
72 hostname <- Sys.info()[["nodename"]]
73 stopifnot(is_localhost(hostname))
74
75 ## After being identified, hostname should be cached
76 stopifnot(is_localhost(hostname))
77
78 ## A clearly non-local host
79 stopifnot(!is_localhost("this-host-does-not-exist-12345.example.org"))
80
81 ## Reset cache for clean state
82 is_localhost(worker = NULL, hostname = NULL)
83
84
85 message("- is_ip_number()")
86
87 is_ip_number <- parallelly:::is_ip_number
88 stopifnot(
89 is_ip_number("192.168.1.1"),
90 is_ip_number("0.0.0.0"),
91 is_ip_number("255.255.255.255"),
92 !is_ip_number("256.0.0.0"),
93 !is_ip_number("not-an-ip"),
94 !is_ip_number("192.168.1"),
95 !is_ip_number("192.168.1.1.1")
96 )
97
98
99 message("- is_fqdn()")
100
101 is_fqdn <- parallelly:::is_fqdn
102 stopifnot(
103 is_fqdn("host.example.com"),
104 is_fqdn("a.b"),
105 !is_fqdn("localhost"),
106 !is_fqdn("singlename")
107 )
108
109 message("*** utils,cluster ... DONE")