forked from r-lib/usethis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-edit.R
161 lines (127 loc) · 4.63 KB
/
test-edit.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
context("edit")
expect_r_file <- function(...) {
expect_true(file_exists(path_home_r(...)))
}
expect_fs_file <- function(...) {
expect_true(file_exists(path_home(...)))
}
test_that("edit_file() creates new directory and another and a file within", {
tmp <- file_temp()
expect_false(dir_exists(tmp))
capture.output(new_file <- edit_file(path(tmp, "new_dir", "new_file")))
expect_true(dir_exists(tmp))
expect_true(dir_exists(path(tmp, "new_dir")))
expect_true(file_exists(path(tmp, "new_dir", "new_file")))
})
test_that("edit_file() creates new file in existing directory", {
tmp <- file_temp()
dir_create(tmp)
capture.output(new_file <- edit_file(path(tmp, "new_file")))
expect_true(file_exists(path(tmp, "new_file")))
})
test_that("edit_file() copes with path to existing file", {
tmp <- file_temp()
dir_create(tmp)
existing <- file_create(path(tmp, "a_file"))
capture.output(res <- edit_file(path(tmp, "a_file")))
expect_identical(existing, res)
})
## testing edit_XXX("user") only on travis and appveyor, because I don't want to
## risk creating user-level files de novo for an actual user, which would
## obligate me to some nerve-wracking clean up
test_that("edit_r_XXX() and edit_git_XXX() have default scope", {
skip_if_no_git_user()
## run these manually if you already have these files or are happy to
## have them or delete them
skip_if_not_ci()
## on Windows, under R CMD check, some env vars are set to sentinel values
## https://github.com/wch/r-source/blob/78da6e06aa0017564ec057b768f98c5c79e4d958/src/library/tools/R/check.R#L257
## we need to explicitly ensure R_ENVIRON_USER="" here
withr::local_envvar(list(R_ENVIRON_USER = ""))
expect_error_free(edit_r_profile())
expect_error_free(edit_r_buildignore())
expect_error_free(edit_r_environ())
expect_error_free(edit_r_makevars())
expect_error_free(edit_git_config())
expect_error_free(edit_git_ignore())
})
test_that("edit_r_XXX('user') ensures the file exists", {
## run these manually if you already have these files or are happy to
## have them or delete them
skip_if_not_ci()
## on Windows, under R CMD check, some env vars are set to sentinel values
## https://github.com/wch/r-source/blob/78da6e06aa0017564ec057b768f98c5c79e4d958/src/library/tools/R/check.R#L257
## we need to explicitly ensure R_ENVIRON_USER="" here
withr::local_envvar(list(R_ENVIRON_USER = ""))
edit_r_environ("user")
expect_r_file(".Renviron")
edit_r_profile("user")
expect_r_file(".Rprofile")
edit_r_buildignore("user")
expect_r_file(".Rbuildignore")
edit_r_makevars("user")
expect_r_file(".R", "Makevars")
edit_rstudio_snippets(type = "R")
expect_r_file(".R", "snippets", "r.snippets")
edit_rstudio_snippets(type = "HTML")
expect_r_file(".R", "snippets", "html.snippets")
expect_error(
edit_rstudio_snippets("not-existing-type"),
regexp = "should be one of"
)
})
test_that("edit_r_profile() respects R_PROFILE_USER", {
path1 <- user_path_prep(file_temp())
withr::local_envvar(list(R_PROFILE_USER = path1))
path2 <- edit_r_profile("user")
expect_equal(path1, as.character(path2))
})
test_that("edit_git_XXX('user') ensures the file exists", {
skip_if_no_git_user()
## run these manually if you already have these files or are happy to
## have them or delete them
skip_if_not_ci()
edit_git_config("user")
expect_fs_file(".gitconfig")
edit_git_ignore("user")
expect_fs_file(".gitignore")
cfg <- git2r::config()
expect_match(cfg$global$core.excludesfile, "gitignore")
})
test_that("edit_r_profile() ensures .Rprofile exists in project", {
create_local_package()
edit_r_profile("project")
expect_proj_file(".Rprofile")
create_local_project()
edit_r_profile("project")
expect_proj_file(".Rprofile")
})
test_that("edit_r_environ() ensures .Renviron exists in project", {
create_local_package()
edit_r_environ("project")
expect_proj_file(".Renviron")
create_local_project()
edit_r_environ("project")
expect_proj_file(".Renviron")
})
test_that("edit_r_makevars() ensures .R/Makevars exists in package", {
create_local_package()
edit_r_makevars("project")
expect_proj_file(".R", "Makevars")
})
test_that("edit_git_config() ensures git ignore file exists in project", {
create_local_package()
edit_git_config("project")
expect_proj_file(".git", "config")
create_local_project()
edit_git_config("project")
expect_proj_file(".git", "config")
})
test_that("edit_git_ignore() ensures .gitignore exists in project", {
create_local_package()
edit_git_ignore("project")
expect_proj_file(".gitignore")
create_local_project()
edit_git_ignore("project")
expect_proj_file(".gitignore")
})