@@ -502,3 +502,76 @@ func TestPath_Parts(t *testing.T) {
502502 })
503503 }
504504}
505+
506+ func TestPath_Copy (t * testing.T ) {
507+ tests := []struct {
508+ name string
509+ srcContents string
510+ dstContents string
511+ wantDstContents string
512+ createDstFile bool
513+ wantErr bool
514+ }{
515+ {
516+ name : "copy empty file to existing non-empty file" ,
517+ srcContents : "" ,
518+ dstContents : "foobar" ,
519+ wantDstContents : "" ,
520+ createDstFile : true ,
521+ },
522+ {
523+ name : "copy empty file to existing empty file" ,
524+ srcContents : "" ,
525+ dstContents : "" ,
526+ wantDstContents : "" ,
527+ createDstFile : true ,
528+ },
529+ {
530+ name : "copy empty file to non-existing file" ,
531+ srcContents : "" ,
532+ wantDstContents : "" ,
533+ createDstFile : false ,
534+ },
535+ {
536+ name : "copy non-empty file to existing non-empty file" ,
537+ srcContents : "foobar" ,
538+ dstContents : "hello world" ,
539+ wantDstContents : "foobar" ,
540+ createDstFile : true ,
541+ },
542+ {
543+ name : "copy non-empty file to existing empty file" ,
544+ srcContents : "foobar" ,
545+ dstContents : "" ,
546+ wantDstContents : "foobar" ,
547+ createDstFile : true ,
548+ },
549+ {
550+ name : "copy non-empty file to non-existing file" ,
551+ srcContents : "foobar" ,
552+ wantDstContents : "foobar" ,
553+ createDstFile : false ,
554+ },
555+ }
556+ for _ , tt := range tests {
557+ t .Run (tt .name , func (t * testing.T ) {
558+ tmpdir := NewPath (t .TempDir ())
559+ src := tmpdir .Join ("src.txt" )
560+ dst := tmpdir .Join ("dst.txt" )
561+ require .NoError (t , src .WriteFile ([]byte (tt .srcContents )))
562+
563+ if tt .createDstFile {
564+ require .NoError (t , dst .WriteFile ([]byte (tt .dstContents )))
565+ }
566+
567+ _ , err := src .Copy (dst )
568+ if ! tt .wantErr {
569+ require .NoError (t , err )
570+ }
571+
572+ dstBytes , err := dst .ReadFile ()
573+ require .NoError (t , err )
574+ assert .Equal (t , []byte (tt .wantDstContents ), dstBytes )
575+ })
576+ }
577+ }
0 commit comments