@@ -2,7 +2,9 @@ package ghtoc
2
2
3
3
import (
4
4
"bytes"
5
+ "errors"
5
6
"log"
7
+ "os"
6
8
"testing"
7
9
)
8
10
@@ -412,3 +414,71 @@ func TestGHDocConvert2HTML(t *testing.T) {
412
414
t .Error ("Wrong html. \n Got :" , doc .html , "\n Want:" , htmlBody )
413
415
}
414
416
}
417
+
418
+ func TestGHDocConvert2HTMLNonPlainText (t * testing.T ) {
419
+ remotePath := "https://github.com/some/readme.md"
420
+ token := "some-gh-token"
421
+ doc := NewGHDoc (remotePath , true , 0 , 0 ,
422
+ true , token , 4 , false )
423
+
424
+ // mock for getting remote raw README text
425
+ htmlResponse := []byte ("raw md text" )
426
+ doc .httpGetter = func (_ string ) ([]byte , string , error ) {
427
+ return htmlResponse , "text/html;utf-8" , nil
428
+ }
429
+ // should not call converter to HTML
430
+ doc .httpPoster = func (urlPath , filePath , token string ) (string , error ) {
431
+ t .Error ("Should not call httpPost (via convertMd2Html)" )
432
+ return "" , nil
433
+ }
434
+ if err := doc .Convert2HTML (); err != nil {
435
+ t .Error ("Got error:" , err )
436
+ }
437
+ if doc .html != string (htmlResponse ) {
438
+ t .Error ("Wrong html. \n Got :" , doc .html , "\n Want:" , string (htmlResponse ))
439
+ }
440
+ }
441
+
442
+ func TestGHDocConvert2HTMLErrorConvert (t * testing.T ) {
443
+ remotePath := "https://github.com/some/readme.md"
444
+ token := "some-gh-token"
445
+ errGet := errors .New ("error from http get" )
446
+ doc := NewGHDoc (remotePath , true , 0 , 0 ,
447
+ true , token , 4 , false )
448
+
449
+ // mock for getting remote raw README text
450
+ doc .httpGetter = func (urlPath string ) ([]byte , string , error ) {
451
+ return nil , "" , errGet
452
+ }
453
+
454
+ err := doc .Convert2HTML ()
455
+ if err == nil {
456
+ t .Error ("Should get error from http get!" )
457
+ }
458
+
459
+ if ! errors .Is (err , errGet ) {
460
+ t .Error ("Wrong error. \n Got :" , err , "\n Want:" , errGet )
461
+ }
462
+ }
463
+
464
+ func TestGHDocConvert2HTMLLocalFileNotExists (t * testing.T ) {
465
+ localPath := "/some/readme.md"
466
+ token := "some-gh-token"
467
+ doc := NewGHDoc (localPath , true , 0 , 0 ,
468
+ true , token , 4 , false )
469
+
470
+ // should not be called
471
+ doc .httpGetter = func (_ string ) ([]byte , string , error ) {
472
+ t .Error ("Should not call httpGet" )
473
+ return nil , "" , nil
474
+ }
475
+
476
+ err := doc .Convert2HTML ()
477
+ if err == nil {
478
+ t .Error ("Should get error from file checking." )
479
+ }
480
+
481
+ if ! errors .Is (err , os .ErrNotExist ) {
482
+ t .Error ("Wrong error. \n Got :" , err , "\n Want:" , os .ErrNotExist )
483
+ }
484
+ }
0 commit comments