@@ -27,8 +27,16 @@ import {PullRequest} from '../src/pull-request';
2727import { TagName } from '../src/util/tag-name' ;
2828import { Version } from '../src/version' ;
2929import assert = require( 'assert' ) ;
30- import { DuplicateReleaseError , GitHubAPIError } from '../src/errors' ;
30+ import {
31+ DuplicateReleaseError ,
32+ GitHubAPIError ,
33+ FileNotFoundError ,
34+ } from '../src/errors' ;
3135import { fail } from 'assert' ;
36+ import { PullRequestBody } from '../src/util/pull-request-body' ;
37+ import { PullRequestTitle } from '../src/util/pull-request-title' ;
38+ import * as codeSuggester from 'code-suggester' ;
39+ import { RawContent } from '../src/updaters/raw-content' ;
3240
3341const fixturesPath = './test/fixtures' ;
3442const sandbox = sinon . createSandbox ( ) ;
@@ -776,4 +784,134 @@ describe('GitHub', () => {
776784 ) ;
777785 } ) ;
778786 } ) ;
787+
788+ describe ( 'createReleasePullRequest' , ( ) => {
789+ it ( 'should update file' , async ( ) => {
790+ const createPullRequestStub = sandbox
791+ . stub ( codeSuggester , 'createPullRequest' )
792+ . resolves ( 1 ) ;
793+ sandbox
794+ . stub ( github , 'getFileContentsOnBranch' )
795+ . withArgs ( 'existing-file' , 'main' )
796+ . resolves ( {
797+ sha : 'abc123' ,
798+ content : 'somecontent' ,
799+ parsedContent : 'somecontent' ,
800+ mode : '100644' ,
801+ } ) ;
802+ sandbox . stub ( github , 'getPullRequest' ) . withArgs ( 1 ) . resolves ( {
803+ title : 'created title' ,
804+ headBranchName : 'release-please--branches--main' ,
805+ baseBranchName : 'main' ,
806+ number : 1 ,
807+ body : 'some body' ,
808+ labels : [ ] ,
809+ files : [ ] ,
810+ } ) ;
811+ const pullRequest = await github . createReleasePullRequest (
812+ {
813+ title : PullRequestTitle . ofTargetBranch ( 'main' ) ,
814+ body : new PullRequestBody ( [ ] ) ,
815+ labels : [ ] ,
816+ headRefName : 'release-please--branches--main' ,
817+ draft : false ,
818+ updates : [
819+ {
820+ path : 'existing-file' ,
821+ createIfMissing : false ,
822+ updater : new RawContent ( 'some content' ) ,
823+ } ,
824+ ] ,
825+ } ,
826+ 'main'
827+ ) ;
828+ expect ( pullRequest . number ) . to . eql ( 1 ) ;
829+ sinon . assert . calledOnce ( createPullRequestStub ) ;
830+ const changes = createPullRequestStub . getCall ( 0 ) . args [ 1 ] ;
831+ expect ( changes ) . to . not . be . undefined ;
832+ expect ( changes ! . size ) . to . eql ( 1 ) ;
833+ expect ( changes ! . get ( 'existing-file' ) ) . to . not . be . undefined ;
834+ } ) ;
835+ it ( 'should handle missing files' , async ( ) => {
836+ const createPullRequestStub = sandbox
837+ . stub ( codeSuggester , 'createPullRequest' )
838+ . resolves ( 1 ) ;
839+ sandbox
840+ . stub ( github , 'getFileContentsOnBranch' )
841+ . withArgs ( 'missing-file' , 'main' )
842+ . rejects ( new FileNotFoundError ( 'missing-file' ) ) ;
843+ sandbox . stub ( github , 'getPullRequest' ) . withArgs ( 1 ) . resolves ( {
844+ title : 'created title' ,
845+ headBranchName : 'release-please--branches--main' ,
846+ baseBranchName : 'main' ,
847+ number : 1 ,
848+ body : 'some body' ,
849+ labels : [ ] ,
850+ files : [ ] ,
851+ } ) ;
852+ const pullRequest = await github . createReleasePullRequest (
853+ {
854+ title : PullRequestTitle . ofTargetBranch ( 'main' ) ,
855+ body : new PullRequestBody ( [ ] ) ,
856+ labels : [ ] ,
857+ headRefName : 'release-please--branches--main' ,
858+ draft : false ,
859+ updates : [
860+ {
861+ path : 'missing-file' ,
862+ createIfMissing : false ,
863+ updater : new RawContent ( 'some content' ) ,
864+ } ,
865+ ] ,
866+ } ,
867+ 'main'
868+ ) ;
869+ expect ( pullRequest . number ) . to . eql ( 1 ) ;
870+ sinon . assert . calledOnce ( createPullRequestStub ) ;
871+ const changes = createPullRequestStub . getCall ( 0 ) . args [ 1 ] ;
872+ expect ( changes ) . to . not . be . undefined ;
873+ expect ( changes ! . size ) . to . eql ( 0 ) ;
874+ } ) ;
875+ it ( 'should create missing file' , async ( ) => {
876+ const createPullRequestStub = sandbox
877+ . stub ( codeSuggester , 'createPullRequest' )
878+ . resolves ( 1 ) ;
879+ sandbox
880+ . stub ( github , 'getFileContentsOnBranch' )
881+ . withArgs ( 'missing-file' , 'main' )
882+ . rejects ( new FileNotFoundError ( 'missing-file' ) ) ;
883+ sandbox . stub ( github , 'getPullRequest' ) . withArgs ( 1 ) . resolves ( {
884+ title : 'created title' ,
885+ headBranchName : 'release-please--branches--main' ,
886+ baseBranchName : 'main' ,
887+ number : 1 ,
888+ body : 'some body' ,
889+ labels : [ ] ,
890+ files : [ ] ,
891+ } ) ;
892+ const pullRequest = await github . createReleasePullRequest (
893+ {
894+ title : PullRequestTitle . ofTargetBranch ( 'main' ) ,
895+ body : new PullRequestBody ( [ ] ) ,
896+ labels : [ ] ,
897+ headRefName : 'release-please--branches--main' ,
898+ draft : false ,
899+ updates : [
900+ {
901+ path : 'missing-file' ,
902+ createIfMissing : true ,
903+ updater : new RawContent ( 'some content' ) ,
904+ } ,
905+ ] ,
906+ } ,
907+ 'main'
908+ ) ;
909+ expect ( pullRequest . number ) . to . eql ( 1 ) ;
910+ sinon . assert . calledOnce ( createPullRequestStub ) ;
911+ const changes = createPullRequestStub . getCall ( 0 ) . args [ 1 ] ;
912+ expect ( changes ) . to . not . be . undefined ;
913+ expect ( changes ! . size ) . to . eql ( 1 ) ;
914+ expect ( changes ! . get ( 'missing-file' ) ) . to . not . be . undefined ;
915+ } ) ;
916+ } ) ;
779917} ) ;
0 commit comments