<commit_before>// Copyright 2015 ThoughtWorks, Inc.

// This file is part of Gauge.

// Gauge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Gauge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Gauge.  If not, see <http://www.gnu.org/licenses/>.

package validation

import (
	"errors"
	"fmt"
	"os"
	"strconv"
	"strings"

	"github.com/getgauge/gauge/api"
	"github.com/getgauge/gauge/config"
	"github.com/getgauge/gauge/conn"
	"github.com/getgauge/gauge/filter"
	"github.com/getgauge/gauge/gauge"
	"github.com/getgauge/gauge/gauge_messages"
	"github.com/getgauge/gauge/logger"
	"github.com/getgauge/gauge/manifest"
	"github.com/getgauge/gauge/parser"
	"github.com/getgauge/gauge/runner"
	"github.com/golang/protobuf/proto"
)

type ValidationErrMaps struct {
	SpecErrs     map[*gauge.Specification][]*StepValidationError
	ScenarioErrs map[*gauge.Scenario][]*StepValidationError
	StepErrs     map[*gauge.Step]*StepValidationError
}

type validator struct {
	manifest           *manifest.Manifest
	specsToExecute     []*gauge.Specification
	runner             runner.Runner
	conceptsDictionary *gauge.ConceptDictionary
}

type specValidator struct {
	specification        *gauge.Specification
	runner               runner.Runner
	conceptsDictionary   *gauge.ConceptDictionary
	stepValidationErrors []*StepValidationError
	stepValidationCache  map[string]*StepValidationError
}

type StepValidationError struct {
	step      *gauge.Step
	message   string
	fileName  string
	errorType *gauge_messages.StepValidateResponse_ErrorType
}

func (s *StepValidationError) Error() string {
	return fmt.Sprintf("%s:%d: %s => '%s'", s.fileName, s.step.LineNo, s.message, s.step.LineText)
}

func Validate(args []string) {
	_, errMap, runner := ValidateSpecs(args)
	runner.Kill()
	if len(errMap.StepErrs) > 0 {
		os.Exit(1)
	}
	logger.Info("No error found.")
}

//TODO : duplicate in execute.go. Need to fix runner init.
func startAPI() runner.Runner {
	sc := api.StartAPI()
	select {
	case runner := <-sc.RunnerChan:
		return runner
	case err := <-sc.ErrorChan:<commit_msg>gauge --validate takes specs dir as default. #438<commit_after>// Copyright 2015 ThoughtWorks, Inc.

// This file is part of Gauge.

// Gauge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Gauge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Gauge.  If not, see <http://www.gnu.org/licenses/>.

package validation

import (
	"errors"
	"fmt"
	"os"
	"strconv"
	"strings"

	"github.com/getgauge/common"
	"github.com/getgauge/gauge/api"
	"github.com/getgauge/gauge/config"
	"github.com/getgauge/gauge/conn"
	"github.com/getgauge/gauge/filter"
	"github.com/getgauge/gauge/gauge"
	"github.com/getgauge/gauge/gauge_messages"
	"github.com/getgauge/gauge/logger"
	"github.com/getgauge/gauge/manifest"
	"github.com/getgauge/gauge/parser"
	"github.com/getgauge/gauge/runner"
	"github.com/golang/protobuf/proto"
)

type ValidationErrMaps struct {
	SpecErrs     map[*gauge.Specification][]*StepValidationError
	ScenarioErrs map[*gauge.Scenario][]*StepValidationError
	StepErrs     map[*gauge.Step]*StepValidationError
}

type validator struct {
	manifest           *manifest.Manifest
	specsToExecute     []*gauge.Specification
	runner             runner.Runner
	conceptsDictionary *gauge.ConceptDictionary
}

type specValidator struct {
	specification        *gauge.Specification
	runner               runner.Runner
	conceptsDictionary   *gauge.ConceptDictionary
	stepValidationErrors []*StepValidationError
	stepValidationCache  map[string]*StepValidationError
}

type StepValidationError struct {
	step      *gauge.Step
	message   string
	fileName  string
	errorType *gauge_messages.StepValidateResponse_ErrorType
}

func (s *StepValidationError) Error() string {
	return fmt.Sprintf("%s:%d: %s => '%s'", s.fileName, s.step.LineNo, s.message, s.step.LineText)
}

func Validate(args []string) {
	if len(args) == 0 {
		args = append(args, common.SpecsDirectoryName)
	}
	_, errMap, runner := ValidateSpecs(args)
	runner.Kill()
	if len(errMap.StepErrs) > 0 {
		os.Exit(1)
	}
	logger.Info("No error found.")
}

//TODO : duplicate in execute.go. Need to fix runner init.
func startAPI() runner.Runner {
	sc := api.StartAPI()
	select {
	case runner := <-sc.RunnerChan:
		return runner
	case err := <-sc.ErrorChan:
{'fraction_non_alphanumeric': 0.10906981241539354, 'fraction_numerical': 0.0034809514600657515, 'mean_word_length': 4.006776379477251, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5899086', 'n_tokens_mistral': 1712, 'n_tokens_neox': 1602, 'n_words': 453}