From d496d6d86b9e5dc9909da6719c62e6a3f05753ab Mon Sep 17 00:00:00 2001 From: Maciej Banas Date: Mon, 30 Sep 2024 11:53:33 +0000 Subject: [PATCH 1/2] Add GITLAB token to job. --- .github/workflows/pkgdown.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/pkgdown.yaml b/.github/workflows/pkgdown.yaml index 4bbce750..7d1ee754 100644 --- a/.github/workflows/pkgdown.yaml +++ b/.github/workflows/pkgdown.yaml @@ -21,6 +21,7 @@ jobs: group: pkgdown-${{ github.event_name != 'pull_request' || github.run_id }} env: GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + GITLAB_PAT_PUBLIC: ${{ secrets.GITLAB_PAT}} permissions: contents: write steps: From e681129ba7b4a2cac61aa71b6c59f47c949c11b1 Mon Sep 17 00:00:00 2001 From: Maciej Banas Date: Mon, 30 Sep 2024 11:54:06 +0000 Subject: [PATCH 2/2] Mock tests further. --- NEWS.md | 1 + R/EngineGraphQLGitLab.R | 5 +++ .../_snaps/get_files_structure-GitHub.md | 8 +++++ .../_snaps/get_files_structure-GitLab.md | 8 +++++ .../_snaps/get_files_structure-GitStats.md | 27 --------------- tests/testthat/helper-fixtures.R | 34 +++++++++++++++++++ .../test-get_files_structure-GitHub.R | 18 ++++++++++ .../test-get_files_structure-GitLab.R | 23 +++++++++++++ .../test-get_files_structure-GitStats.R | 21 ------------ 9 files changed, 97 insertions(+), 48 deletions(-) diff --git a/NEWS.md b/NEWS.md index 53947ed7..be7a48fa 100644 --- a/NEWS.md +++ b/NEWS.md @@ -10,6 +10,7 @@ - Changed message when searching scope is set to scan whole git host (no `orgs` nor `repos` specified) from warning to info ([#456](https://github.com/r-world-devs/GitStats/issues/456)). - Added new CI/CD jobs: deploy to `gh-pages`, lint and check for bumping version. +- Mocked extensively API responses to improve tests and checks progress ([#481](https://github.com/r-world-devs/GitStats/issues/481)]. # GitStats 2.0.2 diff --git a/R/EngineGraphQLGitLab.R b/R/EngineGraphQLGitLab.R index 8a7853f3..7dc0b38a 100644 --- a/R/EngineGraphQLGitLab.R +++ b/R/EngineGraphQLGitLab.R @@ -276,6 +276,7 @@ EngineGraphQLGitLab <- R6::R6Class( repo_name <- url_split[length(url_split)] return(repo_name) }, + get_repos_data = function(org, repos = NULL) { repos_list <- self$get_repos_from_org( org = org @@ -288,6 +289,7 @@ EngineGraphQLGitLab <- R6::R6Class( ) return(result) }, + get_file_blobs_response = function(org, repo, file_paths) { file_blobs_response <- self$gql_response( gql_query = self$gql_query$file_blob_from_repo(), @@ -298,6 +300,7 @@ EngineGraphQLGitLab <- R6::R6Class( ) return(file_blobs_response) }, + get_files_tree_response = function(org, repo, file_path) { files_tree_response <- self$gql_response( gql_query = self$gql_query$files_tree_from_repo(), @@ -308,6 +311,7 @@ EngineGraphQLGitLab <- R6::R6Class( ) return(files_tree_response) }, + get_files_structure_from_repo = function(org, repo, pattern = NULL, depth = Inf) { files_tree_response <- private$get_files_tree_response( org = org, @@ -364,6 +368,7 @@ EngineGraphQLGitLab <- R6::R6Class( } return(files_structure) }, + get_files_and_dirs = function(files_tree_response) { tree_nodes <- files_tree_response$data$project$repository$tree$trees$nodes blob_nodes <- files_tree_response$data$project$repository$tree$blobs$nodes diff --git a/tests/testthat/_snaps/get_files_structure-GitHub.md b/tests/testthat/_snaps/get_files_structure-GitHub.md index 3fba83aa..37945d6e 100644 --- a/tests/testthat/_snaps/get_files_structure-GitHub.md +++ b/tests/testthat/_snaps/get_files_structure-GitHub.md @@ -26,3 +26,11 @@ Output named list() +# get_files_content makes use of files_structure + + Code + files_content <- github_testhost_priv$get_files_content_from_orgs(file_path = NULL, + host_files_structure = test_mocker$use("gh_files_structure_from_orgs")) + Message + i I will make use of files structure stored in GitStats. + diff --git a/tests/testthat/_snaps/get_files_structure-GitLab.md b/tests/testthat/_snaps/get_files_structure-GitLab.md index 0529a865..a4c86492 100644 --- a/tests/testthat/_snaps/get_files_structure-GitLab.md +++ b/tests/testthat/_snaps/get_files_structure-GitLab.md @@ -14,3 +14,11 @@ Message i [Host:GitLab][Engine:GraphQl][Scope:mbtests] Pulling files structure...[files matching pattern: '\.md|\.R']... +# get_files_content makes use of files_structure + + Code + files_content <- gitlab_testhost_priv$get_files_content_from_orgs(file_path = NULL, + host_files_structure = test_mocker$use("gl_files_structure_from_orgs")) + Message + i I will make use of files structure stored in GitStats. + diff --git a/tests/testthat/_snaps/get_files_structure-GitStats.md b/tests/testthat/_snaps/get_files_structure-GitStats.md index 21ec313a..1f0e3190 100644 --- a/tests/testthat/_snaps/get_files_structure-GitStats.md +++ b/tests/testthat/_snaps/get_files_structure-GitStats.md @@ -13,30 +13,3 @@ files_structure <- test_gitstats$get_files_structure(pattern = "\\.md", depth = 2L, verbose = TRUE) -# get_files_content makes use of files_structure - - Code - files_content <- test_gitstats$get_files_content(file_path = NULL, - use_files_structure = TRUE, verbose = TRUE) - Message - i I will make use of files structure stored in GitStats. - i [Host:GitHub][Engine:GraphQl][Scope:r-world-devs] Pulling files from files structure... - i [Host:GitHub][Engine:GraphQl][Scope:openpharma] Pulling files from files structure... - i I will make use of files structure stored in GitStats. - i [Host:GitLab][Engine:GraphQl][Scope:mbtests] Pulling files from files structure... - i [Host:GitLab][Engine:GraphQl][Scope:mbtestapps] Pulling files from files structure... - ---- - - Code - test_gitstats - Output - A GitStats object for 2 hosts: - Hosts: https://api.github.com, https://gitlab.com/api/v4 - Scanning scope: - Organizations: [2] r-world-devs, mbtests - Repositories: [0] - Storage: - character(0) - Files_structure: 2 [files matching pattern: \.md] - diff --git a/tests/testthat/helper-fixtures.R b/tests/testthat/helper-fixtures.R index 113ec8a7..13298240 100644 --- a/tests/testthat/helper-fixtures.R +++ b/tests/testthat/helper-fixtures.R @@ -220,6 +220,40 @@ test_fixtures$github_search_response <- list( ) ) +test_fixtures$gitlab_files_tree_response <- list( + "data" = list( + "project" = list( + "repository" = list( + "tree" = list( + "trees" = list( + "nodes" = list( + list( + "name" = "R" + ), + list( + "name" = "tests" + ) + ) + ), + "blobs" = list( + "nodes" = list( + list( + "name" = "DESCRIPTION" + ), + list( + "name" = "README.md" + ), + list( + "name" = "project_metadata.yaml" + ) + ) + ) + ) + ) + ) + ) +) + test_fixtures$github_files_tree_response <- list( "data" = list( "repository" = list( diff --git a/tests/testthat/test-get_files_structure-GitHub.R b/tests/testthat/test-get_files_structure-GitHub.R index df68bed6..a7d52af9 100644 --- a/tests/testthat/test-get_files_structure-GitHub.R +++ b/tests/testthat/test-get_files_structure-GitHub.R @@ -226,3 +226,21 @@ test_that("get_files_structure pulls files structure for repositories in orgs", }) test_mocker$cache(gh_files_structure_from_orgs) }) + +test_that("get_files_content makes use of files_structure", { + mockery::stub( + github_testhost_priv$get_files_content_from_orgs, + "private$add_repo_api_url", + test_mocker$use("gh_files_table") + ) + expect_snapshot( + files_content <- github_testhost_priv$get_files_content_from_orgs( + file_path = NULL, + host_files_structure = test_mocker$use("gh_files_structure_from_orgs") + ) + ) + expect_files_table( + files_content, + with_cols = "api_url" + ) +}) diff --git a/tests/testthat/test-get_files_structure-GitLab.R b/tests/testthat/test-get_files_structure-GitLab.R index 5b707535..9c5bf19d 100644 --- a/tests/testthat/test-get_files_structure-GitLab.R +++ b/tests/testthat/test-get_files_structure-GitLab.R @@ -8,6 +8,11 @@ test_that("files tree query for GitLab are built properly", { }) test_that("get_files_tree_response() works", { + mockery::stub( + test_graphql_gitlab_priv$get_files_tree_response, + "self$gql_response", + test_fixtures$gitlab_files_tree_response + ) gl_files_tree_response <- test_graphql_gitlab_priv$get_files_tree_response( org = "mbtests", repo = "graphql_tests", @@ -220,3 +225,21 @@ test_that("get_files_structure pulls files structure for repositories in orgs", }) test_mocker$cache(gl_files_structure_from_orgs) }) + +test_that("get_files_content makes use of files_structure", { + mockery::stub( + gitlab_testhost_priv$get_files_content_from_orgs, + "private$add_repo_api_url", + test_mocker$use("gl_files_table") + ) + expect_snapshot( + files_content <- gitlab_testhost_priv$get_files_content_from_orgs( + file_path = NULL, + host_files_structure = test_mocker$use("gl_files_structure_from_orgs") + ) + ) + expect_files_table( + files_content, + with_cols = "api_url" + ) +}) diff --git a/tests/testthat/test-get_files_structure-GitStats.R b/tests/testthat/test-get_files_structure-GitStats.R index 843d6741..2b0da258 100644 --- a/tests/testthat/test-get_files_structure-GitStats.R +++ b/tests/testthat/test-get_files_structure-GitStats.R @@ -47,24 +47,3 @@ test_that("get_files_structure works as expected", { expect_s3_class(files_structure, "files_structure") test_mocker$cache(files_structure) }) - -test_that("get_files_content makes use of files_structure", { - test_gitstats <- create_test_gitstats( - hosts = 2, - inject_files_structure = "files_structure" - ) - expect_snapshot( - files_content <- test_gitstats$get_files_content( - file_path = NULL, - use_files_structure = TRUE, - verbose = TRUE - ) - ) - expect_files_table( - files_content, - with_cols = "api_url" - ) - expect_snapshot( - test_gitstats - ) -})