@@ -3,9 +3,12 @@ package runner
3
3
import (
4
4
"errors"
5
5
"fmt"
6
+ "io/ioutil"
6
7
"net"
7
8
"os"
9
+ "os/exec"
8
10
"os/signal"
11
+ "runtime"
9
12
"strings"
10
13
"syscall"
11
14
"testing"
@@ -640,3 +643,154 @@ func TestWriteDefaultConfig(t *testing.T) {
640
643
641
644
assert .Equal (t , expect , * actual )
642
645
}
646
+
647
+ func TestCheckNilSliceShouldBeenOverwrite (t * testing.T ) {
648
+ port , f := GetPort ()
649
+ f ()
650
+ t .Logf ("port: %d" , port )
651
+
652
+ tmpDir := initTestEnv (t , port )
653
+
654
+ // change dir to tmpDir
655
+ if err := os .Chdir (tmpDir ); err != nil {
656
+ t .Fatal (err )
657
+ }
658
+
659
+ // write easy config file
660
+
661
+ config := `
662
+ [build]
663
+ cmd = "go build ."
664
+ bin = "tmp/main"
665
+ exclude_regex = []
666
+ exclude_dir = ["test"]
667
+ exclude_file = ["main.go"]
668
+
669
+ `
670
+ if err := ioutil .WriteFile (dftTOML , []byte (config ), 0644 ); err != nil {
671
+ t .Fatal (err )
672
+ }
673
+ engine , err := NewEngine (".air.toml" , true )
674
+ if err != nil {
675
+ t .Fatal (err )
676
+ }
677
+ assert .Equal (t , []string {"go" , "tpl" , "tmpl" , "html" }, engine .config .Build .IncludeExt )
678
+ assert .Equal (t , []string {}, engine .config .Build .ExcludeRegex )
679
+ assert .Equal (t , []string {"test" }, engine .config .Build .ExcludeDir )
680
+ // add new config
681
+ assert .Equal (t , []string {"main.go" }, engine .config .Build .ExcludeFile )
682
+ assert .Equal (t , "go build ." , engine .config .Build .Cmd )
683
+
684
+ }
685
+
686
+ func TestShouldIncludeGoTestFile (t * testing.T ) {
687
+ port , f := GetPort ()
688
+ f ()
689
+ t .Logf ("port: %d" , port )
690
+
691
+ tmpDir := initTestEnv (t , port )
692
+ // change dir to tmpDir
693
+ if err := os .Chdir (tmpDir ); err != nil {
694
+ t .Fatal (err )
695
+ }
696
+ writeDefaultConfig ()
697
+
698
+ // write go test file
699
+ file , err := os .Create ("main_test.go" )
700
+ if err != nil {
701
+ t .Fatal (err )
702
+ }
703
+ _ , err = file .WriteString (`package main
704
+
705
+ import "testing"
706
+
707
+ func Test(t *testing.T) {
708
+ t.Log("testing")
709
+ }
710
+ ` )
711
+ // run sed
712
+ // check the file is exist
713
+ if _ , err := os .Stat (dftTOML ); err != nil {
714
+ t .Fatal (err )
715
+ }
716
+ // check is MacOS
717
+ var cmd * exec.Cmd
718
+ if runtime .GOOS == "darwin" {
719
+ cmd = exec .Command ("gsed" , "-i" , "s/\" _test.*go\" //g" , ".air.toml" )
720
+ } else {
721
+ cmd = exec .Command ("sed" , "-i" , "s/\" _test.*go\" //g" , ".air.toml" )
722
+ }
723
+ cmd .Stdout = os .Stdout
724
+ cmd .Stderr = os .Stderr
725
+ if err := cmd .Run (); err != nil {
726
+ t .Fatal (err )
727
+ }
728
+
729
+ time .Sleep (time .Second * 3 )
730
+ engine , err := NewEngine (".air.toml" , false )
731
+ if err != nil {
732
+ t .Fatal (err )
733
+ }
734
+ go func () {
735
+ engine .Run ()
736
+ }()
737
+
738
+ t .Logf ("start change main_test.go" )
739
+ // change file of main_test.go
740
+ // just append a new empty line to main_test.go
741
+ if err = waitingPortReady (t , port , time .Second * 40 ); err != nil {
742
+ t .Fatal (err )
743
+ }
744
+ go func () {
745
+ file , err := os .OpenFile ("main_test.go" , os .O_APPEND | os .O_WRONLY , 0644 )
746
+ if err != nil {
747
+ t .Fatalf ("Should not be fail: %s." , err )
748
+ }
749
+ defer file .Close ()
750
+ _ , err = file .WriteString ("\n " )
751
+ if err != nil {
752
+ t .Fatalf ("Should not be fail: %s." , err )
753
+ }
754
+ }()
755
+ // should Have rebuild
756
+ if err = waitingPortConnectionRefused (t , port , time .Second * 10 ); err != nil {
757
+ t .Fatal (err )
758
+ }
759
+ }
760
+
761
+ func TestCreateNewDir (t * testing.T ) {
762
+ // generate a random port
763
+ port , f := GetPort ()
764
+ f ()
765
+ t .Logf ("port: %d" , port )
766
+
767
+ tmpDir := initTestEnv (t , port )
768
+ // change dir to tmpDir
769
+ err := os .Chdir (tmpDir )
770
+ if err != nil {
771
+ t .Fatalf ("Should not be fail: %s." , err )
772
+ }
773
+ engine , err := NewEngine ("" , true )
774
+ if err != nil {
775
+ t .Fatalf ("Should not be fail: %s." , err )
776
+ }
777
+
778
+ go func () {
779
+ engine .Run ()
780
+ }()
781
+ time .Sleep (time .Second * 2 )
782
+ assert .True (t , checkPortHaveBeenUsed (port ))
783
+
784
+ // create a new dir make dir
785
+ if err = os .Mkdir (tmpDir + "/dir" , 0644 ); err != nil {
786
+ t .Fatal (err )
787
+ }
788
+
789
+ // no need reload
790
+ if err = waitingPortConnectionRefused (t , port , 3 * time .Second ); err == nil {
791
+ t .Fatal ("should raise a error" )
792
+ }
793
+ engine .Stop ()
794
+ time .Sleep (2 * time .Second )
795
+
796
+ }
0 commit comments