1context("submodule.R")
2
3test_that("parse_submodules works with a single submodule", {
4  x <-
5'[submodule "foobar"]
6  path = baz
7  url = http://foo/bar'
8
9  expect_equal(
10    parse_submodules(x),
11    data.frame(
12      submodule = "foobar",
13      path = "baz",
14      url = "http://foo/bar",
15      branch = NA_character_,
16      stringsAsFactors = FALSE))
17})
18
19test_that("parse_submodules works multiple submodules", {
20  y <-
21'[submodule "foobar"]
22  path = baz
23  url = http://foo/bar
24
25[submodule "foofoo"]
26  path = bunny
27  url = http://little/bunny/foofoo
28  branch = forest'
29
30  expect_equal(
31    parse_submodules(y),
32    data.frame(
33      submodule = c("foobar", "foofoo"),
34      path = c("baz", "bunny"),
35      url = c("http://foo/bar", "http://little/bunny/foofoo"),
36      branch = c(NA_character_, "forest"),
37      stringsAsFactors = FALSE))
38})
39
40test_that("parse_submodules warns and returns empty for invalid submodules", {
41  x <-
42'[submodule "foobar"]
43  path = baz'
44
45  expect_warning(regexp = "Invalid submodule definition",
46    expect_equal(
47      parse_submodules(x),
48      list()
49    )
50  )
51
52  y <-
53'[submodule "foobar"]
54  path = baz
55
56[submodule "foofoo"]
57  path = bunny
58  url = http://little/bunny/foofoo'
59
60  expect_warning(regexp = "Invalid submodule definition",
61    expect_equal(
62      parse_submodules(y),
63      list()
64    )
65  )
66
67  z <- '
68  # [submodule "foobar"] this one is commented out
69  # path = baz
70  # url = https://foo/bar'
71
72  expect_equal(
73    parse_submodules(z),
74    list()
75  )
76})
77
78test_that("Can install a repo with a submodule", {
79
80  if (is.null(git_path())) skip("git is not installed")
81
82  dir <- tempfile()
83  dir.create(dir)
84  on.exit(unlink(dir, recursive = TRUE, force = TRUE))
85  writeLines("foo <- 1", file.path(dir, "foo.R"))
86
87  in_dir(dir, {
88    git("init")
89    git(paste("add", "-A", "."))
90    git(paste(
91      # We need to temporarily set the user name and user email,
92      # in case they are not set
93      "-c", "user.name=foobar", "-c", paste0("user.email=", shQuote("<>")),
94      "commit", "-m", shQuote("Initial commit")))
95  })
96
97  sub <- tempfile()
98  dir.create(sub)
99  on.exit(unlink(sub,recursive=TRUE,force=TRUE),add=TRUE)
100
101  module <- file.path(sub, ".gitmodules")
102
103  writeLines(con = module,
104    sprintf(
105'[submodule "foo"]
106	path = R
107	url = file://%s
108[submodule "bar"]
109	path = bar
110	url = file://%s',
111      URLencode(dir),
112      URLencode(dir)
113    )
114  )
115
116  # The bar submodule is in .Rbuildignore, so we will not fetch it
117  build_ignore <- file.path(sub, ".Rbuildignore")
118  writeLines("^bar$", build_ignore)
119
120
121  update_submodules(sub, NULL, quiet = TRUE)
122  expect_true(dir.exists(file.path(sub, "R")))
123  expect_false(dir.exists(file.path(sub, "bar")))
124
125  # Now remove the R directory so we can try installing the full package
126  unlink(file.path(sub, "R"), recursive = TRUE, force = TRUE)
127
128  # Install the package to a temporary library and verify it works
129  lib <- tempfile()
130  on.exit(unlink(lib, recursive = TRUE, force = TRUE), add = TRUE)
131  dir.create(lib)
132
133  DESC_file <- file.path(sub,"DESCRIPTION")
134  writeLines("Package: submodule\nVersion: 0.0.0.9000",DESC_file)
135
136  install_local(sub, lib = lib, quiet = TRUE)
137  withr::with_libpaths(lib,
138    expect_equal(submodule::foo, 1)
139  )
140})
141
142test_that("Can update a submodule with an empty .gitmodules submodule", {
143
144  if (is.null(git_path())) skip("git is not installed")
145
146  dir <- tempfile()
147  dir.create(dir)
148  on.exit(unlink(dir, recursive = TRUE, force = TRUE))
149
150  sub <- tempfile()
151  dir.create(sub)
152  on.exit(unlink(sub, recursive=TRUE, force=TRUE), add=TRUE)
153
154  module <- file.path(sub, ".gitmodules")
155
156  writeLines(con = module,text = "")
157
158  # The bar submodule is in .Rbuildignore, so we will not fetch it
159  build_ignore <- file.path(sub, ".Rbuildignore")
160
161  writeLines("^bar$", build_ignore)
162
163  expect_error(
164    update_submodules(sub, NULL, quiet = TRUE),
165    NA
166  )
167})
168