From ffa6d6b03a82c53d27ec6771e95eb1e3e89da228 Mon Sep 17 00:00:00 2001 From: Oleg Grenrus Date: Fri, 25 Dec 2015 23:34:42 +0200 Subject: [PATCH] Make test runner more robust --- .travis.yml | 6 +++++- README.md | 9 +++++++++ fixtures/user.json | 32 ++++++++++++++++++++++++++++++++ github.cabal | 6 +++++- spec/Github/UsersSpec.hs | 24 ++++++++++++++++++++---- 5 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 fixtures/user.json diff --git a/.travis.yml b/.travis.yml index ab3b6279..8767e557 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,6 +11,10 @@ before_cache: - rm -fv $HOME/.cabal/packages/hackage.haskell.org/build-reports.log - rm -fv $HOME/.cabal/packages/hackage.haskell.org/00-index.tar +env: + global: + secure: "J522iO9HE8yIMwcbBBCXYiAJ0bZSzMjq9nue6oockCIZc3kSoF/IGhfBMns3KRrzMR7nD0IirpHB0jjSXIQLA+51SaWS8HG2+pI+U3qvCoeDIaB85Iyhk932GIymdySCH7ypw71AeuJTErvZ67TR3m+o98BBk8WgMCcLkGykWKA=" + matrix: include: - env: CABALVER=1.18 GHCVER=7.8.4 @@ -73,7 +77,7 @@ script: - if [ -f configure.ac ]; then autoreconf -i; fi - cabal configure --enable-tests --enable-benchmarks -v2 # -v2 provides useful information for debugging - cabal build # this builds all libraries and executables (including tests/benchmarks) - - cabal test + - cabal test --show-details=always - if [ "$CABALVER" = "1.22" ]; then cabal check; fi - if [ "$CABALVER" = "1.22" ]; then cabal haddock; fi diff --git a/README.md b/README.md index 8932fec5..49274db6 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,15 @@ No support? Not sure. See `DetailedOwner` to know what data could be provided. +Test setup +========== + +To run integration part of tests, you'll need [github access token](https://github.com/settings/tokens/new) +Token is needed, because unauthorised access is highly limited. +It's enough to add only basic read access for public information. + +With `travis encrypt --org --repo yournick/github "GITHUB_TOKEN=yourtoken"` command you get a secret, +you can use in your travis setup to run the test-suite there. Contributions ============= diff --git a/fixtures/user.json b/fixtures/user.json new file mode 100644 index 00000000..ab58bf99 --- /dev/null +++ b/fixtures/user.json @@ -0,0 +1,32 @@ +{ + "login": "mike-burns", + "id": 4550, + "avatar_url": "https://avatars.githubusercontent.com/u/4550?v=3", + "gravatar_id": "", + "url": "https://api.github.com/users/mike-burns", + "html_url": "https://github.com/mike-burns", + "followers_url": "https://api.github.com/users/mike-burns/followers", + "following_url": "https://api.github.com/users/mike-burns/following{/other_user}", + "gists_url": "https://api.github.com/users/mike-burns/gists{/gist_id}", + "starred_url": "https://api.github.com/users/mike-burns/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/mike-burns/subscriptions", + "organizations_url": "https://api.github.com/users/mike-burns/orgs", + "repos_url": "https://api.github.com/users/mike-burns/repos", + "events_url": "https://api.github.com/users/mike-burns/events{/privacy}", + "received_events_url": "https://api.github.com/users/mike-burns/received_events", + "type": "User", + "site_admin": false, + "name": "Mike Burns", + "company": "thoughtbot", + "blog": "http://mike-burns.com/", + "location": "Stockholm, Sweden", + "email": "mburns@thoughtbot.com", + "hireable": true, + "bio": null, + "public_repos": 35, + "public_gists": 32, + "followers": 171, + "following": 0, + "created_at": "2008-04-03T17:54:24Z", + "updated_at": "2015-10-02T16:53:25Z" +} diff --git a/github.cabal b/github.cabal index 5ad03c24..d783f56e 100644 --- a/github.cabal +++ b/github.cabal @@ -181,7 +181,7 @@ Library Build-depends: base >= 4.0 && < 5.0, time >=1.4 && <1.6, aeson >= 0.6.1.0, - aeson-extra >= 0.2.0.0, + aeson-extra >= 0.2.0.0 && <0.3, attoparsec >= 0.10.3.0, bytestring, case-insensitive >= 0.4.0.4, @@ -210,9 +210,13 @@ test-suite github-test default-language: Haskell2010 type: exitcode-stdio-1.0 hs-source-dirs: spec + other-modules: + Github.UsersSpec main-is: Spec.hs build-depends: base >= 4.0 && < 5.0, + aeson-extra >= 0.2.0.0 && <0.3, github, + file-embed, hspec ghc-options: -Wall -fno-warn-orphans diff --git a/spec/Github/UsersSpec.hs b/spec/Github/UsersSpec.hs index bbda6db1..b2dc9f1d 100644 --- a/spec/Github/UsersSpec.hs +++ b/spec/Github/UsersSpec.hs @@ -1,17 +1,33 @@ +{-# LANGUAGE TemplateHaskell #-} module Github.UsersSpec where -import Github.Users (userInfoFor) +import Github.Auth (GithubAuth(..)) +import Github.Users (userInfoFor') import Github.Data.Definitions (DetailedOwner(..)) -import Test.Hspec (it, describe, shouldBe, Spec) +import Data.Aeson.Compat (eitherDecodeStrict) +import Test.Hspec (it, describe, shouldBe, pendingWith, Spec) +import System.Environment (lookupEnv) +import Data.FileEmbed (embedFile) fromRightS :: Show a => Either a b -> b fromRightS (Right b) = b fromRightS (Left a) = error $ "Expected a Right and got a Left" ++ show a +withAuth :: (GithubAuth -> IO ()) -> IO () +withAuth action = do + mtoken <- lookupEnv "GITHUB_TOKEN" + case mtoken of + Nothing -> pendingWith "no GITHUB_TOKEN" + Just token -> action (GithubOAuth token) + spec :: Spec spec = describe "userInfoFor" $ do - it "returns information about the user" $ do - userInfo <- userInfoFor "mike-burns" + it "decodes user json" $ do + let userInfo = eitherDecodeStrict $(embedFile "fixtures/user.json") :: Either String DetailedOwner + detailedOwnerLogin (fromRightS userInfo) `shouldBe` "mike-burns" + + it "returns information about the user" $ withAuth $ \auth -> do + userInfo <- userInfoFor' (Just auth) "mike-burns" detailedOwnerLogin (fromRightS userInfo) `shouldBe` "mike-burns"