<commit_before>#lang racket
(provide generate-coveralls-coverage)
(require racket/runtime-path json "format-utils.rkt")

(module+ test
  (require rackunit "../cover.rkt" racket/runtime-path))

;; Coveralls

;; Coverage [path-string] -> Void
(define-runtime-path post "curl.sh")
(define (generate-coveralls-coverage coverage [dir "coverage"])
  (make-directory* dir)
  (define coverage-path (path->string (build-path (current-directory) dir)))
  (define coverage-file (string-append coverage-path "/coverage.json"))
  (define json (generate-coveralls-json coverage (hasheq)))
  (define meta-data (determine-build-type))
  (with-output-to-file coverage-file
    (thunk (write-json (for/fold ([blob json]) ([(k v) meta-data]) (hash-set blob k v))))
    #:exists 'replace)
  (system* (path->string post) coverage-file))

;; Maps service name to the environment variable that indicates that the service is to be used.
(define BUILD-TYPES (hash "travis_ci" "TRAVIS_BUILD_ID"))

;; -> [Hasheq String String
;; Determine the type of build (e.g. repo token, travis, etc) and return the appropriate metadata
(define (determine-build-type)
  (define service-name (for/first ([(name var) BUILD-TYPES] #:when (getenv var)) name))
  (define repo-token (getenv "COVERALLS_REPO_TOKEN"))
  (cond [(and service-name repo-token)
         (hasheq 'service_name service-name
                 'service_job_id (getenv (hash-ref BUILD-TYPES service-name))
                 'repo_token repo-token)]
        [repo-token (hasheq 'service_name "cover" 'repo_token repo-token)]
        [else (error "No repo token or ci service detected")]))

;; Coverage [Hasheq String String] -> JSexpr
;; Generates a string that represents a valid coveralls json_file object
(define (generate-coveralls-json coverage meta)
  (define src-files<commit_msg>no need for the repo token, I hope<commit_after>#lang racket
(provide generate-coveralls-coverage)
(require racket/runtime-path json "format-utils.rkt")

(module+ test
  (require rackunit "../cover.rkt" racket/runtime-path))

;; Coveralls

;; Coverage [path-string] -> Void
(define-runtime-path post "curl.sh")
(define (generate-coveralls-coverage coverage [dir "coverage"])
  (make-directory* dir)
  (define coverage-path (path->string (build-path (current-directory) dir)))
  (define coverage-file (string-append coverage-path "/coverage.json"))
  (define json (generate-coveralls-json coverage (hasheq)))
  (define meta-data (determine-build-type))
  (with-output-to-file coverage-file
    (thunk (write-json (for/fold ([blob json]) ([(k v) meta-data]) (hash-set blob k v))))
    #:exists 'replace)
  (system* (path->string post) coverage-file))

;; Maps service name to the environment variable that indicates that the service is to be used.
(define BUILD-TYPES (hash "travis_ci" "TRAVIS_BUILD_ID"))

;; -> [Hasheq String String
;; Determine the type of build (e.g. repo token, travis, etc) and return the appropriate metadata
(define (determine-build-type)
  (define service-name (for/first ([(name var) BUILD-TYPES] #:when (getenv var)) name))
  (define repo-token (getenv "COVERALLS_REPO_TOKEN"))
  (cond [service-name
         (hasheq 'service_name service-name
                 'service_job_id (getenv (hash-ref BUILD-TYPES service-name)))]
        [repo-token (hasheq 'service_name "cover" 'repo_token repo-token)]
        [else (error "No repo token or ci service detected")]))

;; Coverage [Hasheq String String] -> JSexpr
;; Generates a string that represents a valid coveralls json_file object
(define (generate-coveralls-json coverage meta)
  (define src-files
{'fraction_non_alphanumeric': 0.13552704963747908, 'fraction_numerical': 0.0, 'mean_word_length': 4.298375184638109, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5409815', 'n_tokens_mistral': 1136, 'n_tokens_neox': 1124, 'n_words': 346}