diff --git a/tests/testthat/test-codecov.R b/tests/testthat/test-codecov.R index 83ef36e6..afae87f3 100644 --- a/tests/testthat/test-codecov.R +++ b/tests/testthat/test-codecov.R @@ -54,396 +54,332 @@ ci_vars <- c( cov <- package_coverage(test_path("TestS4")) test_that("it generates a properly formatted json file", { - withr::with_envvar( - ci_vars, - with_mocked_bindings( - RETRY = function(...) list(...), - content = identity, - local_branch = function(dir) "master", - current_commit = function(dir) "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3", - code = { - res <- codecov(coverage = cov) - json <- jsonlite::fromJSON(res$body) + withr::local_envvar(ci_vars) + local_mocked_bindings( + RETRY = function(...) list(...), + content = identity, + local_branch = function(dir) "master", + current_commit = function(dir) "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" + ) + res <- codecov(coverage = cov) + json <- jsonlite::fromJSON(res$body) - expect_match(json$files$name, "R/TestS4.R") - expect_equal( - json$files$coverage[[1]], - c( - NA, NA, NA, NA, NA, NA, NA, 5, 2, NA, 3, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, - NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, 1, NA - ) - ) - expect_equal(json$uploader, "R") - } + expect_match(json$files$name, "R/TestS4.R") + expect_equal( + json$files$coverage[[1]], + c( + NA, NA, NA, NA, NA, NA, NA, 5, 2, NA, 3, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, + NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, 1, NA ) ) + expect_equal(json$uploader, "R") }) test_that("it adds a flags argument to the query if specified", { - withr::with_envvar( - ci_vars, - with_mocked_bindings( - RETRY = function(...) list(...), - content = identity, - local_branch = function(dir) "master", - current_commit = function(dir) "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3", - code = { - res <- codecov(coverage = cov, flags = "R") - expect_equal(res$query$flags, "R") - } - ) + withr::local_envvar(ci_vars) + local_mocked_bindings( + RETRY = function(...) list(...), + content = identity, + local_branch = function(dir) "master", + current_commit = function(dir) "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" ) + res <- codecov(coverage = cov, flags = "R") + expect_equal(res$query$flags, "R") }) test_that("it works with local repos", { - withr::with_envvar(ci_vars, { - with_mocked_bindings( - RETRY = function(...) list(...), - content = identity, - local_branch = function(dir) "master", - current_commit = function(dir) "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3", - code = { - res <- codecov(coverage = cov) + withr::local_envvar(ci_vars) + local_mocked_bindings( + RETRY = function(...) list(...), + content = identity, + local_branch = function(dir) "master", + current_commit = function(dir) "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" + ) + res <- codecov(coverage = cov) - expect_match(res$url, "2") # nolint - expect_match(res$query$branch, "master") - expect_match(res$query$commit, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3") - } - ) - }) + expect_match(res$url, "2") # nolint + expect_match(res$query$branch, "master") + expect_match(res$query$commit, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3") }) test_that("it works with local repos and explicit branch and commit", { - withr::with_envvar(ci_vars, { - with_mocked_bindings( - RETRY = function(...) list(...), - content = identity, - code = { - res <- codecov(coverage = cov, branch = "master", commit = "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3") + withr::local_envvar(ci_vars) + local_mocked_bindings( + RETRY = function(...) list(...), + content = identity + ) + res <- codecov(coverage = cov, branch = "master", commit = "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3") - expect_match(res$url, "/upload/v2") # nolint - expect_match(res$query$branch, "master") - expect_match(res$query$commit, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3") - } - ) - }) + expect_match(res$url, "/upload/v2") # nolint + expect_match(res$query$branch, "master") + expect_match(res$query$commit, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3") }) test_that("it adds the token to the query if available", { - withr::with_envvar( - c( - ci_vars, - "CODECOV_TOKEN" = "codecov_test" - ), - with_mocked_bindings( - RETRY = function(...) list(...), - content = identity, - local_branch = function(dir) "master", - current_commit = function(dir) "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3", - code = { - res <- codecov(coverage = cov) - - expect_match(res$url, "/upload/v2") # nolint - expect_match(res$query$branch, "master") - expect_match(res$query$commit, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3") - expect_match(res$query$token, "codecov_test") - } - ) + withr::local_envvar(c(ci_vars, "CODECOV_TOKEN" = "codecov_test")) + local_mocked_bindings( + RETRY = function(...) list(...), + content = identity, + local_branch = function(dir) "master", + current_commit = function(dir) "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" ) + res <- codecov(coverage = cov) + + expect_match(res$url, "/upload/v2") # nolint + expect_match(res$query$branch, "master") + expect_match(res$query$commit, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3") + expect_match(res$query$token, "codecov_test") }) test_that("it looks for token in a .yml file", { - withr::with_envvar( - c(ci_vars), - with_mocked_bindings( - RETRY = function(...) list(...), - content = identity, - local_branch = function(dir) "master", - current_commit = function(dir) "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3", - code = { - res <- codecov(coverage = cov) - - expect_match(res$url, "/upload/v2") # nolint - expect_match(res$query$branch, "master") - expect_match(res$query$commit, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3") - expect_match(res$query$token, "codecov_token_from_yaml") - } - ) + withr::local_envvar(ci_vars) + local_mocked_bindings( + RETRY = function(...) list(...), + content = identity, + local_branch = function(dir) "master", + current_commit = function(dir) "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" ) + res <- codecov(coverage = cov) + + expect_match(res$url, "/upload/v2") # nolint + expect_match(res$query$branch, "master") + expect_match(res$query$commit, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3") + expect_match(res$query$token, "codecov_token_from_yaml") }) test_that("it works with jenkins", { - withr::with_envvar( - c( - ci_vars, - "JENKINS_URL" = "jenkins.com", - "GIT_BRANCH" = "test", - "GIT_COMMIT" = "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3", - "BUILD_NUMBER" = "1", - "BUILD_URL" = "http://test.com/tester/test" - ), - with_mocked_bindings( - RETRY = function(...) list(...), - content = identity, - code = { - res <- codecov(coverage = cov) - - expect_match(res$query$service, "jenkins") - expect_match(res$query$branch, "test") - expect_match(res$query$commit, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3") - expect_match(res$query$build, "1") - expect_match(res$query$build_url, "http://test.com/tester/test") - } - ) + withr::local_envvar(c( + ci_vars, + "JENKINS_URL" = "jenkins.com", + "GIT_BRANCH" = "test", + "GIT_COMMIT" = "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3", + "BUILD_NUMBER" = "1", + "BUILD_URL" = "http://test.com/tester/test" + )) + local_mocked_bindings( + RETRY = function(...) list(...), + content = identity ) + res <- codecov(coverage = cov) + + expect_match(res$query$service, "jenkins") + expect_match(res$query$branch, "test") + expect_match(res$query$commit, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3") + expect_match(res$query$build, "1") + expect_match(res$query$build_url, "http://test.com/tester/test") }) test_that("it works with travis normal builds", { - withr::with_envvar( - c( - ci_vars, - "CI" = "true", - "TRAVIS" = "true", - "TRAVIS_PULL_REQUEST" = "false", - "TRAVIS_REPO_SLUG" = "tester/test", - "TRAVIS_BRANCH" = "master", - "TRAVIS_JOB_NUMBER" = "100", - "TRAVIS_JOB_ID" = "10", - "TRAVIS_COMMIT" = "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" - ), - with_mocked_bindings( - RETRY = function(...) list(...), - content = identity, - code = { - res <- codecov(coverage = cov) - expect_match(res$query$service, "travis") - expect_match(res$query$branch, "master") - expect_match(res$query$job, "10") - expect_match(res$query$pr, "") - expect_match(res$query$slug, "tester/test") - expect_match(res$query$commit, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3") - expect_match(res$query$build, "100") - } - ) + withr::local_envvar(c( + ci_vars, + "CI" = "true", + "TRAVIS" = "true", + "TRAVIS_PULL_REQUEST" = "false", + "TRAVIS_REPO_SLUG" = "tester/test", + "TRAVIS_BRANCH" = "master", + "TRAVIS_JOB_NUMBER" = "100", + "TRAVIS_JOB_ID" = "10", + "TRAVIS_COMMIT" = "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" + )) + local_mocked_bindings( + RETRY = function(...) list(...), + content = identity ) + res <- codecov(coverage = cov) + + expect_match(res$query$service, "travis") + expect_match(res$query$branch, "master") + expect_match(res$query$job, "10") + expect_match(res$query$pr, "") + expect_match(res$query$slug, "tester/test") + expect_match(res$query$commit, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3") + expect_match(res$query$build, "100") }) test_that("it works with travis pull requests", { - withr::with_envvar( - c( - ci_vars, - "CI" = "true", - "TRAVIS" = "true", - "TRAVIS_PULL_REQUEST" = "5", - "TRAVIS_REPO_SLUG" = "tester/test", - "TRAVIS_BRANCH" = "master", - "TRAVIS_JOB_NUMBER" = "100", - "TRAVIS_JOB_ID" = "10", - "TRAVIS_COMMIT" = "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" - ), - with_mocked_bindings( - RETRY = function(...) list(...), - content = identity, - code = { - res <- codecov(coverage = cov) - - expect_match(res$query$service, "travis") - expect_match(res$query$branch, "master") - expect_match(res$query$job, "10") - expect_match(res$query$pr, "5") - expect_match(res$query$slug, "tester/test") - expect_match(res$query$commit, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3") - expect_match(res$query$build, "100") - } - ) + withr::local_envvar(c( + ci_vars, + "CI" = "true", + "TRAVIS" = "true", + "TRAVIS_PULL_REQUEST" = "5", + "TRAVIS_REPO_SLUG" = "tester/test", + "TRAVIS_BRANCH" = "master", + "TRAVIS_JOB_NUMBER" = "100", + "TRAVIS_JOB_ID" = "10", + "TRAVIS_COMMIT" = "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" + )) + local_mocked_bindings( + RETRY = function(...) list(...), + content = identity ) + res <- codecov(coverage = cov) + + expect_match(res$query$service, "travis") + expect_match(res$query$branch, "master") + expect_match(res$query$job, "10") + expect_match(res$query$pr, "5") + expect_match(res$query$slug, "tester/test") + expect_match(res$query$commit, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3") + expect_match(res$query$build, "100") }) test_that("it works with codeship", { - withr::with_envvar( - c( - ci_vars, - "CI" = "true", - "CI_NAME" = "codeship", - "CI_BRANCH" = "master", - "CI_BUILD_NUMBER" = "5", - "CI_BUILD_URL" = "http://test.com/tester/test", - "CI_COMMIT_ID" = "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" - ), - with_mocked_bindings( - RETRY = function(...) list(...), - content = identity, - code = { - res <- codecov(coverage = cov) - - expect_match(res$query$service, "codeship") - expect_match(res$query$branch, "master") - expect_match(res$query$build, "5") - expect_match(res$query$build_url, "http://test.com/tester/test") - expect_match(res$query$commit, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3") - } - ) + withr::local_envvar(c( + ci_vars, + "CI" = "true", + "CI_NAME" = "codeship", + "CI_BRANCH" = "master", + "CI_BUILD_NUMBER" = "5", + "CI_BUILD_URL" = "http://test.com/tester/test", + "CI_COMMIT_ID" = "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" + )) + local_mocked_bindings( + RETRY = function(...) list(...), + content = identity ) + res <- codecov(coverage = cov) + + expect_match(res$query$service, "codeship") + expect_match(res$query$branch, "master") + expect_match(res$query$build, "5") + expect_match(res$query$build_url, "http://test.com/tester/test") + expect_match(res$query$commit, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3") }) test_that("it works with circleci", { - withr::with_envvar( - c( - ci_vars, - "CI" = "true", - "CIRCLECI" = "true", - "CIRCLE_BRANCH" = "master", - "CIRCLE_BUILD_NUM" = "5", - "CIRCLE_PROJECT_USERNAME" = "tester", - "CIRCLE_PROJECT_REPONAME" = "test", - "CIRCLE_SHA1" = "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" - ), - with_mocked_bindings( - RETRY = function(...) list(...), - content = identity, - code = { - res <- codecov(coverage = cov) - - expect_match(res$query$service, "circleci") - expect_match(res$query$branch, "master") - expect_match(res$query$build, "5") - expect_match(res$query$owner, "tester") - expect_match(res$query$repo, "test") - expect_match(res$query$commit, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3") - } - ) + withr::local_envvar(c( + ci_vars, + "CI" = "true", + "CIRCLECI" = "true", + "CIRCLE_BRANCH" = "master", + "CIRCLE_BUILD_NUM" = "5", + "CIRCLE_PROJECT_USERNAME" = "tester", + "CIRCLE_PROJECT_REPONAME" = "test", + "CIRCLE_SHA1" = "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" + )) + local_mocked_bindings( + RETRY = function(...) list(...), + content = identity ) + res <- codecov(coverage = cov) + + expect_match(res$query$service, "circleci") + expect_match(res$query$branch, "master") + expect_match(res$query$build, "5") + expect_match(res$query$owner, "tester") + expect_match(res$query$repo, "test") + expect_match(res$query$commit, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3") }) test_that("it works with semaphore", { - withr::with_envvar( - c( - ci_vars, - "CI" = "true", - "SEMAPHORE" = "true", - "BRANCH_NAME" = "master", - "SEMAPHORE_BUILD_NUMBER" = "5", - "SEMAPHORE_REPO_SLUG" = "tester/test", - "REVISION" = "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" - ), - with_mocked_bindings( - RETRY = function(...) list(...), - content = identity, - code = { - res <- codecov(coverage = cov) - - expect_match(res$query$service, "semaphore") - expect_match(res$query$branch, "master") - expect_match(res$query$build, "5") - expect_match(res$query$owner, "tester") - expect_match(res$query$repo, "test") - expect_match(res$query$commit, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3") - } - ) + withr::local_envvar(c( + ci_vars, + "CI" = "true", + "SEMAPHORE" = "true", + "BRANCH_NAME" = "master", + "SEMAPHORE_BUILD_NUMBER" = "5", + "SEMAPHORE_REPO_SLUG" = "tester/test", + "REVISION" = "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" + )) + local_mocked_bindings( + RETRY = function(...) list(...), + content = identity ) + res <- codecov(coverage = cov) + + expect_match(res$query$service, "semaphore") + expect_match(res$query$branch, "master") + expect_match(res$query$build, "5") + expect_match(res$query$owner, "tester") + expect_match(res$query$repo, "test") + expect_match(res$query$commit, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3") }) test_that("it works with drone", { - withr::with_envvar( - c( - ci_vars, - "CI" = "true", - "DRONE" = "true", - "DRONE_BRANCH" = "master", - "DRONE_BUILD_NUMBER" = "5", - "DRONE_BUILD_URL" = "http://test.com/tester/test", - "DRONE_COMMIT" = "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" - ), - with_mocked_bindings( - RETRY = function(...) list(...), - content = identity, - code = { - res <- codecov(coverage = cov) - - expect_match(res$query$service, "drone.io") - expect_match(res$query$branch, "master") - expect_match(res$query$build, "5") - expect_match(res$query$build_url, "http://test.com/tester/test") - expect_match(res$query$commit, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3") - } - ) + withr::local_envvar(c( + ci_vars, + "CI" = "true", + "DRONE" = "true", + "DRONE_BRANCH" = "master", + "DRONE_BUILD_NUMBER" = "5", + "DRONE_BUILD_URL" = "http://test.com/tester/test", + "DRONE_COMMIT" = "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" + )) + local_mocked_bindings( + RETRY = function(...) list(...), + content = identity ) + res <- codecov(coverage = cov) + + expect_match(res$query$service, "drone.io") + expect_match(res$query$branch, "master") + expect_match(res$query$build, "5") + expect_match(res$query$build_url, "http://test.com/tester/test") + expect_match(res$query$commit, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3") }) test_that("it works with AppVeyor", { - withr::with_envvar( - c( - ci_vars, - "CI" = "True", - "APPVEYOR" = "True", - "APPVEYOR_REPO_NAME" = "testspace/test", - "APPVEYOR_REPO_BRANCH" = "master", - "APPVEYOR_ACCOUNT_NAME" = "testuser", # not necessarily the same as testspace above - "APPVEYOR_PROJECT_SLUG" = "test", - "APPVEYOR_BUILD_VERSION" = "1.0.5", - "APPVEYOR_JOB_ID" = "225apqggpmlkn5pr", - "APPVEYOR_REPO_COMMIT" = "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" - ), - with_mocked_bindings( - RETRY = function(...) list(...), - content = identity, - code = { - res <- codecov(coverage = cov) - - expect_match(res$query$service, "appveyor") - expect_match(res$query$branch, "master") - expect_match(res$query$job, "testuser/test/1.0.5") - expect_match(res$query$build, "225apqggpmlkn5pr") - expect_match(res$query$slug, "testspace/test") - expect_match(res$query$commit, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3") - } - ) + withr::local_envvar(c( + ci_vars, + "CI" = "True", + "APPVEYOR" = "True", + "APPVEYOR_REPO_NAME" = "testspace/test", + "APPVEYOR_REPO_BRANCH" = "master", + "APPVEYOR_ACCOUNT_NAME" = "testuser", # not necessarily the same as testspace above + "APPVEYOR_PROJECT_SLUG" = "test", + "APPVEYOR_BUILD_VERSION" = "1.0.5", + "APPVEYOR_JOB_ID" = "225apqggpmlkn5pr", + "APPVEYOR_REPO_COMMIT" = "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" + )) + local_mocked_bindings( + RETRY = function(...) list(...), + content = identity ) + res <- codecov(coverage = cov) + + expect_match(res$query$service, "appveyor") + expect_match(res$query$branch, "master") + expect_match(res$query$job, "testuser/test/1.0.5") + expect_match(res$query$build, "225apqggpmlkn5pr") + expect_match(res$query$slug, "testspace/test") + expect_match(res$query$commit, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3") }) test_that("it works with Wercker", { - withr::with_envvar( - c( - ci_vars, - "CI" = "true", - "WERCKER_GIT_BRANCH" = "master", - "WERCKER_MAIN_PIPELINE_STARTED" = "5", - "WERCKER_GIT_OWNER" = "tester", - "WERCKER_GIT_REPOSITORY" = "test", - "WERCKER_GIT_COMMIT" = "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" - ), - with_mocked_bindings( - RETRY = function(...) list(...), - content = identity, - code = { - res <- codecov(coverage = cov) - - expect_match(res$query$service, "wercker") - expect_match(res$query$branch, "master") - expect_match(res$query$build, "5") - expect_match(res$query$owner, "tester") - expect_match(res$query$repo, "test") - expect_match(res$query$commit, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3") - } - ) + withr::local_envvar(c( + ci_vars, + "CI" = "true", + "WERCKER_GIT_BRANCH" = "master", + "WERCKER_MAIN_PIPELINE_STARTED" = "5", + "WERCKER_GIT_OWNER" = "tester", + "WERCKER_GIT_REPOSITORY" = "test", + "WERCKER_GIT_COMMIT" = "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" + )) + local_mocked_bindings( + RETRY = function(...) list(...), + content = identity ) + res <- codecov(coverage = cov) + + expect_match(res$query$service, "wercker") + expect_match(res$query$branch, "master") + expect_match(res$query$build, "5") + expect_match(res$query$owner, "tester") + expect_match(res$query$repo, "test") + expect_match(res$query$commit, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3") }) test_that("it works with GitLab", { - withr::with_envvar( - c( - ci_vars, - "CI" = "true", - "CI_SERVER_NAME" = "GitLab CI", - "CI_BUILD_ID" = "5", - "CI_BUILD_REPO" = "https://gitlab.com/tester/test.git", - "CI_BUILD_REF_NAME" = "master", - "CI_BUILD_REF" = "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" - ), - with_mocked_bindings( - RETRY = function(...) list(...), - content = identity, - code = { - res <- codecov(coverage = cov) - - expect_match(res$query$service, "gitlab") - expect_match(res$query$branch, "master") - expect_match(res$query$build, "5") - expect_match(res$query$slug, "tester/test") - expect_match(res$query$commit, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3") - } - ) + withr::local_envvar(c( + ci_vars, + "CI" = "true", + "CI_SERVER_NAME" = "GitLab CI", + "CI_BUILD_ID" = "5", + "CI_BUILD_REPO" = "https://gitlab.com/tester/test.git", + "CI_BUILD_REF_NAME" = "master", + "CI_BUILD_REF" = "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" + )) + local_mocked_bindings( + RETRY = function(...) list(...), + content = identity ) + res <- codecov(coverage = cov) + + expect_match(res$query$service, "gitlab") + expect_match(res$query$branch, "master") + expect_match(res$query$build, "5") + expect_match(res$query$slug, "tester/test") + expect_match(res$query$commit, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3") }) diff --git a/tests/testthat/test-coveralls.R b/tests/testthat/test-coveralls.R index e7f48c18..20ab9638 100644 --- a/tests/testthat/test-coveralls.R +++ b/tests/testthat/test-coveralls.R @@ -51,121 +51,116 @@ read_file <- function(file) paste(collapse = "\n", readLines(file)) cov <- package_coverage(test_path("TestS4")) test_that("coveralls generates a properly formatted json file", { - withr::with_envvar( - c(ci_vars, "CI_NAME" = "FAKECI"), - with_mocked_bindings( - RETRY = function(...) list(...), - content = identity, - upload_file = function(file) readChar(file, file.info(file)$size), - code = { - res <- coveralls(coverage = cov) - json <- jsonlite::fromJSON(res$body$json_file) + withr::local_envvar(c( + ci_vars, + "CI_NAME" = "FAKECI" + )) + local_mocked_bindings( + RETRY = function(...) list(...), + content = identity, + upload_file = function(file) readChar(file, file.info(file)$size) + ) + res <- coveralls(coverage = cov) + json <- jsonlite::fromJSON(res$body$json_file) - expect_equal(nrow(json$source_files), 1) - expect_equal(json$service_name, "fakeci") - expect_match(json$source_files$name, rex::rex("R", one_of("/", "\\"), "TestS4.R")) - expect_equal(json$source_files$source, read_file("TestS4/R/TestS4.R")) - expect_equal(json$source_files$source_digest, "1233f2eca5d84704101cb9d9b928f2e9") - expect_equal( - json$source_files$coverage[[1]], - c( - NA, NA, NA, NA, NA, NA, 5, 2, NA, 3, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, - NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, 1, NA - ) - ) - } + expect_equal(nrow(json$source_files), 1) + expect_equal(json$service_name, "fakeci") + expect_match(json$source_files$name, rex::rex("R", one_of("/", "\\"), "TestS4.R")) + expect_equal(json$source_files$source, read_file("TestS4/R/TestS4.R")) + expect_equal(json$source_files$source_digest, "1233f2eca5d84704101cb9d9b928f2e9") + expect_equal( + json$source_files$coverage[[1]], + c( + NA, NA, NA, NA, NA, NA, 5, 2, NA, 3, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, + NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, 1, NA ) ) }) test_that("coveralls can spawn a job using repo_token", { - withr::with_envvar( - c(ci_vars, "CI_NAME" = "DRONE"), - with_mocked_bindings( - RETRY = function(...) list(...), - content = identity, - upload_file = function(file) readChar(file, file.info(file)$size), - system_output = function(...) paste0(c("a", "b", "c", "d", "e", "f"), collapse = "\n"), - code = { - res <- coveralls(coverage = cov, repo_token = "mytoken") - json <- jsonlite::fromJSON(res$body$json_file) + withr::local_envvar(c( + ci_vars, + "CI_NAME" = "DRONE" + )) + local_mocked_bindings( + RETRY = function(...) list(...), + content = identity, + upload_file = function(file) readChar(file, file.info(file)$size), + system_output = function(...) paste0(c("a", "b", "c", "d", "e", "f"), collapse = "\n") + ) + res <- coveralls(coverage = cov, repo_token = "mytoken") + json <- jsonlite::fromJSON(res$body$json_file) - expect_equal(is.null(json$git), FALSE) - expect_equal(nrow(json$source_files), 1) - # service_name set #285 - expect_equal(json$service_name, "drone") - expect_equal(json$repo_token, "mytoken") - expect_match(json$source_files$name, rex::rex("R", one_of("/", "\\"), "TestS4.R")) - expect_equal(json$source_files$source, read_file("TestS4/R/TestS4.R")) - expect_equal(json$source_files$source_digest, "1233f2eca5d84704101cb9d9b928f2e9") - expect_equal( - json$source_files$coverage[[1]], - c( - NA, NA, NA, NA, NA, NA, 5, 2, NA, 3, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, - NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, 1, NA - ) - ) - } + expect_equal(is.null(json$git), FALSE) + expect_equal(nrow(json$source_files), 1) + # service_name set #285 + expect_equal(json$service_name, "drone") + expect_equal(json$repo_token, "mytoken") + expect_match(json$source_files$name, rex::rex("R", one_of("/", "\\"), "TestS4.R")) + expect_equal(json$source_files$source, read_file("TestS4/R/TestS4.R")) + expect_equal(json$source_files$source_digest, "1233f2eca5d84704101cb9d9b928f2e9") + expect_equal( + json$source_files$coverage[[1]], + c( + NA, NA, NA, NA, NA, NA, 5, 2, NA, 3, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, + NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, 1, NA ) ) }) test_that("generates correct payload for Drone and Jenkins", { - withr::with_envvar( - c(ci_vars, "CI_NAME" = "FAKECI", "CI_BRANCH" = "fakebranch", "CI_REMOTE" = "covr"), - with_mocked_bindings( - system_output = function(...) paste0(c("a", "b", "c", "d", "e", "f"), collapse = "\n"), - code = { - git <- jenkins_git_info() - - expect_equal(git$head$id, jsonlite::unbox("a")) - expect_equal(git$head$author_name, jsonlite::unbox("b")) - expect_equal(git$head$author_email, jsonlite::unbox("c")) - expect_equal(git$head$commiter_name, jsonlite::unbox("d")) - expect_equal(git$head$commiter_email, jsonlite::unbox("e")) - expect_equal(git$head$message, jsonlite::unbox("f")) - expect_equal(git$branch, jsonlite::unbox("fakebranch")) - expect_equal(git$remotes[[1]]$name, jsonlite::unbox("origin")) - expect_equal(git$remotes[[1]]$url, jsonlite::unbox("covr")) - } - ) + withr::local_envvar(c( + ci_vars, + "CI_NAME" = "FAKECI", "CI_BRANCH" = "fakebranch", "CI_REMOTE" = "covr" + )) + local_mocked_bindings( + system_output = function(...) paste0(c("a", "b", "c", "d", "e", "f"), collapse = "\n") ) + git <- jenkins_git_info() + + expect_equal(git$head$id, jsonlite::unbox("a")) + expect_equal(git$head$author_name, jsonlite::unbox("b")) + expect_equal(git$head$author_email, jsonlite::unbox("c")) + expect_equal(git$head$commiter_name, jsonlite::unbox("d")) + expect_equal(git$head$commiter_email, jsonlite::unbox("e")) + expect_equal(git$head$message, jsonlite::unbox("f")) + expect_equal(git$branch, jsonlite::unbox("fakebranch")) + expect_equal(git$remotes[[1]]$name, jsonlite::unbox("origin")) + expect_equal(git$remotes[[1]]$url, jsonlite::unbox("covr")) }) test_that("coveralls can spawn a job using repo_token - travis-pro #285", { - withr::with_envvar( - c(ci_vars, "CI_NAME" = "travis-pro"), - with_mocked_bindings( - RETRY = function(...) list(...), - content = identity, - upload_file = function(file) readChar(file, file.info(file)$size), - system_output = function(...) paste0(c("a", "b", "c", "d", "e", "f"), collapse = "\n"), - code = { - res <- coveralls(coverage = cov, repo_token = "mytoken") - json <- jsonlite::fromJSON(res$body$json_file) + withr::local_envvar( + c(ci_vars, "CI_NAME" = "travis-pro") + ) + local_mocked_bindings( + RETRY = function(...) list(...), + content = identity, + upload_file = function(file) readChar(file, file.info(file)$size), + system_output = function(...) paste0(c("a", "b", "c", "d", "e", "f"), collapse = "\n") + ) + res <- coveralls(coverage = cov, repo_token = "mytoken") + json <- jsonlite::fromJSON(res$body$json_file) - expect_equal(is.null(json$git), FALSE) - expect_equal(nrow(json$source_files), 1) - expect_equal(json$service_name, "travis-pro") - expect_equal(json$repo_token, "mytoken") - expect_match(json$source_files$name, rex::rex("R", one_of("/", "\\"), "TestS4.R")) - expect_equal(json$source_files$source, read_file("TestS4/R/TestS4.R")) - expect_equal(json$source_files$source_digest, "1233f2eca5d84704101cb9d9b928f2e9") - expect_equal( - json$source_files$coverage[[1]], - c( - NA, NA, NA, NA, NA, NA, 5, 2, NA, 3, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, - NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, 1, NA - ) - ) - # git correct #285 - expect_equal(json$git$head$id, "a") - expect_equal(json$git$head$author_name, "b") - expect_equal(json$git$head$author_email, "c") - expect_equal(json$git$head$commiter_name, "d") - expect_equal(json$git$head$commiter_email, "e") - expect_equal(json$git$head$message, "f") - } + expect_equal(is.null(json$git), FALSE) + expect_equal(nrow(json$source_files), 1) + expect_equal(json$service_name, "travis-pro") + expect_equal(json$repo_token, "mytoken") + expect_match(json$source_files$name, rex::rex("R", one_of("/", "\\"), "TestS4.R")) + expect_equal(json$source_files$source, read_file("TestS4/R/TestS4.R")) + expect_equal(json$source_files$source_digest, "1233f2eca5d84704101cb9d9b928f2e9") + expect_equal( + json$source_files$coverage[[1]], + c( + NA, NA, NA, NA, NA, NA, 5, 2, NA, 3, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, + NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, 1, NA, NA, NA, NA, NA, 1, NA ) ) + # git correct #285 + expect_equal(json$git$head$id, "a") + expect_equal(json$git$head$author_name, "b") + expect_equal(json$git$head$author_email, "c") + expect_equal(json$git$head$commiter_name, "d") + expect_equal(json$git$head$commiter_email, "e") + expect_equal(json$git$head$message, "f") }) diff --git a/tests/testthat/test-gcov.R b/tests/testthat/test-gcov.R index ec2ee0c5..a99da5ee 100644 --- a/tests/testthat/test-gcov.R +++ b/tests/testthat/test-gcov.R @@ -1,18 +1,16 @@ test_that("parse_gcov parses files properly", { local_mocked_bindings( - # Only called within parse_gcov + # functions called within parse_gcov file.exists = function(path) TRUE, - # Only called within normalize_path normalize_path = function(path) "simple.c", - # Only called within parse_gcov line_coverages = function(source_file, matches, values, ...) values ) with_mocked_bindings( - expect_equal(parse_gcov("hi.c.gcov"), numeric()), readLines = function(x) { " -: 0:Source:simple.c" - } + }, + expect_equal(parse_gcov("hi.c.gcov"), numeric()) ) with_mocked_bindings( @@ -41,7 +39,7 @@ test_that("parse_gcov parses files properly", { " 4: 6:SEXP simple_(SEXP x) {" ) }, - code = expect_equal(parse_gcov("hi.c.gcov"), 4) + expect_equal(parse_gcov("hi.c.gcov"), 4) ) with_mocked_bindings( readLines = function(x) { @@ -61,7 +59,7 @@ test_that("parse_gcov parses files properly", { " #####: 8: pout[0] = 0;" ) }, - code = expect_equal(parse_gcov("hi.c.gcov"), c(4, 0)) + expect_equal(parse_gcov("hi.c.gcov"), c(4, 0)) ) })