1+ import fs from "fs" ;
12import got from "got" ;
3+ import path from "path" ;
4+ import fixturez from "fixturez" ;
5+ import { promisify } from "util" ;
26import fetchBitbucketPipeline , { getTotalBuilds } from "../bitbucket" ;
37
8+ const readFile = promisify ( fs . readFile ) ;
9+ const BITBUCKET_API = "https://api.bitbucket.org/2.0/repositories" ;
10+
411jest . mock ( "got" ) ;
12+ const f = fixturez ( __dirname ) ;
513
614describe ( "bitbucket" , ( ) => {
715 describe ( "getTotalBuild" , ( ) => {
@@ -25,4 +33,52 @@ describe("bitbucket", () => {
2533 ) . rejects . toThrow ( "The Authorisation failed" ) ;
2634 } ) ;
2735 } ) ;
36+
37+ describe ( "fetchBitbucketPipeline" , ( ) => {
38+ it ( "should write the build info in standard format" , async ( ) => {
39+ const testUser = "test-user" ;
40+ const testRepo = "test-repo" ;
41+ const tempDir = f . temp ( ) ;
42+ got . mockImplementation ( async ( url ) => {
43+ if ( url === `${ BITBUCKET_API } /${ testUser } /${ testRepo } /pipelines/` )
44+ return Promise . resolve ( { body : JSON . stringify ( { size : 1 } ) } ) ;
45+ if (
46+ url ===
47+ `${ BITBUCKET_API } /${ testUser } /${ testRepo } /pipelines/?pagelen=100&page=1&sort=created_on`
48+ ) {
49+ const path_to_pipelines_response = f . find (
50+ "bitbucket_pipeline_response.json"
51+ ) ;
52+ const pipeline_response = await readFile (
53+ path_to_pipelines_response ,
54+ "utf-8"
55+ ) ;
56+
57+ return {
58+ body : JSON . stringify ( JSON . parse ( pipeline_response ) ) ,
59+ } ;
60+ }
61+ } ) ;
62+
63+ await fetchBitbucketPipeline ( tempDir , {
64+ concurrency : 1 ,
65+ repo : testRepo ,
66+ since : "4" ,
67+ user : testUser ,
68+ } ) ;
69+
70+ const pipelineData = await readFile (
71+ path . resolve ( tempDir , "1.json" ) ,
72+ "utf-8"
73+ ) ;
74+
75+ expect ( JSON . parse ( pipelineData ) ) . toEqual ( {
76+ id : 1 ,
77+ uuid : 1 ,
78+ createdOn : "" ,
79+ duration : 17 ,
80+ result : "SUCCESSFUL" ,
81+ } ) ;
82+ } ) ;
83+ } ) ;
2884} ) ;
0 commit comments