-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathtest-submodule.R
165 lines (131 loc) · 3.75 KB
/
test-submodule.R
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
test_that("parse_submodules works with a single submodule", {
x <-
'[submodule "foobar"]
path = baz
url = http://foo/bar'
expect_equal(
parse_submodules(x),
data.frame(
submodule = "foobar",
path = "baz",
url = "http://foo/bar",
branch = NA_character_,
stringsAsFactors = FALSE))
})
test_that("parse_submodules works multiple submodules", {
y <-
'[submodule "foobar"]
path = baz
url = http://foo/bar
[submodule "foofoo"]
path = bunny
url = http://little/bunny/foofoo
branch = forest'
expect_equal(
parse_submodules(y),
data.frame(
submodule = c("foobar", "foofoo"),
path = c("baz", "bunny"),
url = c("http://foo/bar", "http://little/bunny/foofoo"),
branch = c(NA_character_, "forest"),
stringsAsFactors = FALSE))
})
test_that("parse_submodules warns and returns empty for invalid submodules", {
x <-
'[submodule "foobar"]
path = baz'
expect_warning(regexp = "Invalid submodule definition",
expect_equal(
parse_submodules(x),
list()
)
)
y <-
'[submodule "foobar"]
path = baz
[submodule "foofoo"]
path = bunny
url = http://little/bunny/foofoo'
expect_warning(regexp = "Invalid submodule definition",
expect_equal(
parse_submodules(y),
list()
)
)
z <- '
# [submodule "foobar"] this one is commented out
# path = baz
# url = https://foo/bar'
expect_equal(
parse_submodules(z),
list()
)
})
test_that("Can install a repo with a submodule", {
if (is.null(git_path())) skip("git is not installed")
dir <- tempfile()
dir.create(dir)
on.exit(unlink(dir, recursive = TRUE, force = TRUE))
writeLines("foo <- 1", file.path(dir, "foo.R"))
in_dir(dir, {
git("init")
git(paste("add", "-A", "."))
git(paste(
# We need to temporarily set the user name and user email,
# in case they are not set
"-c", "user.name=foobar", "-c", paste0("user.email=", shQuote("<>")),
"commit", "-m", shQuote("Initial commit")))
})
sub <- tempfile()
dir.create(sub)
on.exit(unlink(sub,recursive=TRUE,force=TRUE),add=TRUE)
module <- file.path(sub, ".gitmodules")
writeLines(con = module,
sprintf(
'[submodule "foo"]
path = R
url = file://%s
[submodule "bar"]
path = bar
url = file://%s',
URLencode(dir),
URLencode(dir)
)
)
# The bar submodule is in .Rbuildignore, so we will not fetch it
build_ignore <- file.path(sub, ".Rbuildignore")
writeLines("^bar$", build_ignore)
update_submodules(sub, NULL, quiet = TRUE)
expect_true(dir.exists(file.path(sub, "R")))
expect_false(dir.exists(file.path(sub, "bar")))
# Now remove the R directory so we can try installing the full package
unlink(file.path(sub, "R"), recursive = TRUE, force = TRUE)
# Install the package to a temporary library and verify it works
lib <- tempfile()
on.exit(unlink(lib, recursive = TRUE, force = TRUE), add = TRUE)
dir.create(lib)
DESC_file <- file.path(sub,"DESCRIPTION")
writeLines("Package: submodule\nVersion: 0.0.0.9000",DESC_file)
install_local(sub, lib = lib, quiet = TRUE)
withr::with_libpaths(lib,
expect_equal(submodule::foo, 1)
)
})
test_that("Can update a submodule with an empty .gitmodules submodule", {
if (is.null(git_path())) skip("git is not installed")
dir <- tempfile()
dir.create(dir)
on.exit(unlink(dir, recursive = TRUE, force = TRUE))
sub <- tempfile()
dir.create(sub)
on.exit(unlink(sub, recursive=TRUE, force=TRUE), add=TRUE)
module <- file.path(sub, ".gitmodules")
writeLines(con = module,text = "")
# The bar submodule is in .Rbuildignore, so we will not fetch it
build_ignore <- file.path(sub, ".Rbuildignore")
writeLines("^bar$", build_ignore)
expect_error(
update_submodules(sub, NULL, quiet = TRUE),
NA
)
})