How to use watchForFileChanges method of infoGatherer Package

Best Gauge code snippet using infoGatherer.watchForFileChanges

specDetails.go

Source:specDetails.go Github

copy

Full Screen

...43	return d.Spec != nil && d.Spec.Heading != nil44}45// MakeListOfAvailableSteps initializes all the SpecInfoGatherer caches46func (s *SpecInfoGatherer) MakeListOfAvailableSteps() {47	go s.watchForFileChanges()48	s.waitGroup.Wait()49	// Concepts parsed first because we need to create a concept dictionary that spec parsing can use50	s.waitGroup.Add(3)51	s.initConceptsCache()52	s.initSpecsCache()53	s.initStepsCache()54}55func (s *SpecInfoGatherer) initSpecsCache() {56	defer s.waitGroup.Done()57	s.specsCache = make(map[string]*SpecDetail, 0)58	details := s.getParsedSpecs(getSpecFiles(s.SpecDirs))59	logger.APILog.Info("Initializing specs cache with %d specs", len(details))60	for _, d := range details {61		logger.APILog.Debug("Adding specs from %s", d.Spec.FileName)62		s.addToSpecsCache(d.Spec.FileName, d)63	}64}65func getSpecFiles(specs []string) []string {66	var specFiles []string67	for _, dir := range specs {68		specFiles = append(specFiles, util.FindSpecFilesIn(dir)...)69	}70	return specFiles71}72func (s *SpecInfoGatherer) initConceptsCache() {73	defer s.waitGroup.Done()74	s.conceptsCache = make(map[string][]*gauge.Concept, 0)75	parsedConcepts := s.getParsedConcepts()76	logger.APILog.Info("Initializing concepts cache with %d concepts", len(parsedConcepts))77	for _, concept := range parsedConcepts {78		logger.APILog.Debug("Adding concepts from %s", concept.FileName)79		s.addToConceptsCache(concept.FileName, concept)80	}81}82func (s *SpecInfoGatherer) initStepsCache() {83	defer s.waitGroup.Done()84	s.stepsCache = make(map[string]*gauge.StepValue, 0)85	stepsFromSpecs := s.getStepsFromCachedSpecs()86	stepsFromConcepts := s.getStepsFromCachedConcepts()87	allSteps := append(stepsFromSpecs, stepsFromConcepts...)88	logger.APILog.Info("Initializing steps cache with %d steps", len(allSteps))89	s.addToStepsCache(allSteps)90}91func (s *SpecInfoGatherer) addToSpecsCache(key string, value *SpecDetail) {92	s.mutex.Lock()93	s.specsCache[key] = value94	s.mutex.Unlock()95}96func (s *SpecInfoGatherer) addToConceptsCache(key string, value *gauge.Concept) {97	s.mutex.Lock()98	if s.conceptsCache[key] == nil {99		s.conceptsCache[key] = make([]*gauge.Concept, 0)100	}101	s.conceptsCache[key] = append(s.conceptsCache[key], value)102	s.mutex.Unlock()103}104func (s *SpecInfoGatherer) addToStepsCache(allSteps []*gauge.StepValue) {105	s.mutex.Lock()106	for _, step := range allSteps {107		if _, ok := s.stepsCache[step.StepValue]; !ok {108			s.stepsCache[step.StepValue] = step109		}110	}111	s.mutex.Unlock()112}113func (s *SpecInfoGatherer) getParsedSpecs(specFiles []string) []*SpecDetail {114	if s.conceptDictionary == nil {115		s.conceptDictionary = gauge.NewConceptDictionary()116	}117	parsedSpecs, parseResults := parser.ParseSpecFiles(specFiles, s.conceptDictionary, gauge.NewBuildErrors())118	specs := make(map[string]*SpecDetail)119	for _, spec := range parsedSpecs {120		specs[spec.FileName] = &SpecDetail{Spec: spec}121	}122	for _, v := range parseResults {123		_, ok := specs[v.FileName]124		if !ok {125			specs[v.FileName] = &SpecDetail{Spec: &gauge.Specification{FileName: v.FileName}}126		}127		specs[v.FileName].Errs = append(v.CriticalErrors, v.ParseErrors...)128	}129	details := make([]*SpecDetail, 0)130	for _, d := range specs {131		details = append(details, d)132	}133	return details134}135func (s *SpecInfoGatherer) getParsedConcepts() map[string]*gauge.Concept {136	var result *parser.ParseResult137	s.conceptDictionary, result = parser.CreateConceptsDictionary()138	handleParseFailures([]*parser.ParseResult{result})139	return s.conceptDictionary.ConceptsMap140}141func (s *SpecInfoGatherer) getStepsFromCachedSpecs() []*gauge.StepValue {142	var stepValues []*gauge.StepValue143	s.mutex.Lock()144	for _, detail := range s.specsCache {145		stepValues = append(stepValues, getStepsFromSpec(detail.Spec)...)146	}147	s.mutex.Unlock()148	return stepValues149}150func (s *SpecInfoGatherer) getStepsFromCachedConcepts() []*gauge.StepValue {151	var stepValues []*gauge.StepValue152	s.mutex.Lock()153	for _, conceptList := range s.conceptsCache {154		for _, concept := range conceptList {155			stepValues = append(stepValues, getStepsFromConcept(concept)...)156		}157	}158	s.mutex.Unlock()159	return stepValues160}161func (s *SpecInfoGatherer) onSpecFileModify(file string) {162	s.waitGroup.Add(1)163	defer s.waitGroup.Done()164	logger.APILog.Info("Spec file added / modified: %s", file)165	details := s.getParsedSpecs([]string{file})166	s.addToSpecsCache(file, details[0])167	stepsFromSpec := getStepsFromSpec(details[0].Spec)168	s.addToStepsCache(stepsFromSpec)169}170func (s *SpecInfoGatherer) onConceptFileModify(file string) {171	s.waitGroup.Add(1)172	defer s.waitGroup.Done()173	logger.APILog.Info("Concept file added / modified: %s", file)174	conceptParser := new(parser.ConceptParser)175	concepts, parseResults := conceptParser.ParseFile(file)176	if parseResults != nil && len(parseResults.ParseErrors) > 0 {177		for _, err := range parseResults.ParseErrors {178			logger.APILog.Error("Error parsing concepts: ", err)179		}180		return181	}182	for _, concept := range concepts {183		c := gauge.Concept{ConceptStep: concept, FileName: file}184		s.addToConceptsCache(file, &c)185		stepsFromConcept := getStepsFromConcept(&c)186		s.addToStepsCache(stepsFromConcept)187	}188}189func (s *SpecInfoGatherer) onSpecFileRemove(file string) {190	s.waitGroup.Add(1)191	defer s.waitGroup.Done()192	logger.APILog.Info("Spec file removed: %s", file)193	s.mutex.Lock()194	delete(s.specsCache, file)195	s.mutex.Unlock()196}197func (s *SpecInfoGatherer) onConceptFileRemove(file string) {198	s.waitGroup.Add(1)199	defer s.waitGroup.Done()200	logger.APILog.Info("Concept file removed: %s", file)201	s.mutex.Lock()202	delete(s.conceptsCache, file)203	s.mutex.Unlock()204}205func (s *SpecInfoGatherer) onFileAdd(watcher *fsnotify.Watcher, file string) {206	if util.IsDir(file) {207		addDirToFileWatcher(watcher, file)208	}209	s.onFileModify(watcher, file)210}211func (s *SpecInfoGatherer) onFileModify(watcher *fsnotify.Watcher, file string) {212	if util.IsSpec(file) {213		s.onSpecFileModify(file)214	} else if util.IsConcept(file) {215		s.onConceptFileModify(file)216	}217}218func (s *SpecInfoGatherer) onFileRemove(watcher *fsnotify.Watcher, file string) {219	if util.IsSpec(file) {220		s.onSpecFileRemove(file)221	} else if util.IsConcept(file) {222		s.onConceptFileRemove(file)223	} else {224		removeWatcherOn(watcher, file)225	}226}227func (s *SpecInfoGatherer) onFileRename(watcher *fsnotify.Watcher, file string) {228	s.onFileRemove(watcher, file)229}230func (s *SpecInfoGatherer) handleEvent(event fsnotify.Event, watcher *fsnotify.Watcher) {231	s.waitGroup.Wait()232	file, err := filepath.Abs(event.Name)233	if err != nil {234		logger.APILog.Error("Failed to get abs file path for %s: %s", event.Name, err)235		return236	}237	if util.IsSpec(file) || util.IsConcept(file) || util.IsDir(file) {238		switch event.Op {239		case fsnotify.Create:240			s.onFileAdd(watcher, file)241		case fsnotify.Write:242			s.onFileModify(watcher, file)243		case fsnotify.Rename:244			s.onFileRename(watcher, file)245		case fsnotify.Remove:246			s.onFileRemove(watcher, file)247		}248	}249}250func (s *SpecInfoGatherer) watchForFileChanges() {251	s.waitGroup.Add(1)252	watcher, err := fsnotify.NewWatcher()253	if err != nil {254		logger.APILog.Error("Error creating fileWatcher: %s", err)255	}256	defer watcher.Close()257	done := make(chan bool)258	go func() {259		for {260			select {261			case event := <-watcher.Events:262				s.handleEvent(event, watcher)263			case err := <-watcher.Errors:264				logger.APILog.Error("Error event while watching specs", err)...

Full Screen

Full Screen

watchForFileChanges

Using AI Code Generation

copy

Full Screen

1func main() {2    infoGatherer := infoGatherer{}3    infoGatherer.watchForFileChanges()4}5func main() {6    infoGatherer := infoGatherer{}7    infoGatherer.watchForFileChanges()8}9import (10func main() {11    infoGatherer := infoGatherer{}12    infoGatherer.watchForFileChanges()13}14import (15func main() {16    infoGatherer := infoGatherer{}17    infoGatherer.watchForFileChanges()18}

Full Screen

Full Screen

watchForFileChanges

Using AI Code Generation

copy

Full Screen

1import (2func main() {3	watcher, err := fsnotify.NewWatcher()4	if err != nil {5		log.Fatal(err)6	}7	defer watcher.Close()8	if err := watcher.Add("."); err != nil {9		log.Fatal(err)10	}11	go func() {12		for {13			select {14				{15					fmt.Println("event:", event)16					if event.Op&fsnotify.Write == fsnotify.Write {17						fmt.Println("modified file:", event.Name)18					}19				}20				{21					fmt.Println("error:", err)22				}23			}24		}25	}()26	sig := make(chan os.Signal, 1)27	signal.Notify(sig, os.Interrupt, syscall.SIGTERM)28	time.Sleep(5 * time.Second)29}30import (31func main() {32	watcher, err := fsnotify.NewWatcher()33	if err != nil {34		log.Fatal(err)35	}36	defer watcher.Close()37	if err := watcher.Add("."); err != nil {38		log.Fatal(err)39	}40	go func() {41		for {42			select {43				{44					fmt.Println("event:", event)45					if event.Op&fsnotify.Write == fsnotify.Write {46						fmt.Println("modified file:", event.Name)47					}48				}49				{

Full Screen

Full Screen

watchForFileChanges

Using AI Code Generation

copy

Full Screen

1func main() {2    var infoGathererObj = infoGatherer{}3    infoGathererObj.watchForFileChanges()4}5func main() {6    var infoGathererObj = infoGatherer{}7    infoGathererObj.watchForFileChanges()8}9func main() {10    var infoGathererObj = infoGatherer{}11    infoGathererObj.watchForFileChanges()12}13func main() {14    var infoGathererObj = infoGatherer{}15    infoGathererObj.watchForFileChanges()16}17func main() {18    var infoGathererObj = infoGatherer{}19    infoGathererObj.watchForFileChanges()20}21func main() {22    var infoGathererObj = infoGatherer{}23    infoGathererObj.watchForFileChanges()24}25func main() {26    var infoGathererObj = infoGatherer{}

Full Screen

Full Screen

watchForFileChanges

Using AI Code Generation

copy

Full Screen

1import (2func main() {3	path, _ := os.Getwd()4	fmt.Println(path)5	info := watchForFileChanges(path)6	fmt.Println(info)7}8func watchForFileChanges(path string) string {9	watcher, err := fsnotify.NewWatcher()10	if err != nil {11		fmt.Println("ERROR", err)12	}13	defer watcher.Close()14	err = watcher.Add(path)15	if err != nil {16		fmt.Println("ERROR", err)17	}18	for {19		select {20			{21				if event.Op&fsnotify.Write == fsnotify.Write {22					fmt.Println("modified file:", event.Name)23					info = getInfo(path, event.Name)24				}25			}26			{27				fmt.Println("ERROR", err)28			}29		}30	}31}32func getInfo(path string, name string) string {33	fileName := filepath.Base(name)34	fileExt := filepath.Ext(name)35	fileSize := getFileSize(name)36	fileModTime := getFileModTime(name)37	fileAccessTime := getFileAccessTime(name)38	fileChangeTime := getFileChangeTime(name)39	filePermission := getFilePermission(name)40	fileInode := getFileInode(name)

Full Screen

Full Screen

watchForFileChanges

Using AI Code Generation

copy

Full Screen

1import (2func main() {3	infoGatherer.WatchForFileChanges()4	fmt.Println("Hello, playground")5}6import (7func main() {8	infoGatherer.GetDiskUsage()9	fmt.Println("Hello, playground")10}11import (12func main() {13	infoGatherer.GetFileInfo()14	fmt.Println("Hello, playground")15}16import (17func main() {18	infoGatherer.GetDirectoryInfo()19	fmt.Println("Hello, playground")20}21import (22func main() {23	infoGatherer.ListFiles()24	fmt.Println("Hello, playground")25}26import (27func main() {28	infoGatherer.ListDirectories()29	fmt.Println("Hello, playground")30}31import (32func main() {33	infoGatherer.GetFileInfo()34	fmt.Println("Hello, playground")35}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Gauge automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful