Ejemplo n.º 1
0
        public override void DiscoverTests(
            IEnumerable <string> sources,
            PythonProjectSettings settings,
            IMessageLogger logger,
            ITestCaseDiscoverySink discoverySink
            )
        {
            if (sources is null)
            {
                throw new ArgumentNullException(nameof(sources));
            }

            if (discoverySink is null)
            {
                throw new ArgumentNullException(nameof(discoverySink));
            }

            _logger = logger ?? throw new ArgumentNullException(nameof(logger));

            var workspaceText = settings.IsWorkspace ? Strings.WorkspaceText : Strings.ProjectText;

            LogInfo(Strings.PythonTestDiscovererStartedMessage.FormatUI(PythonConstants.PytestText, settings.ProjectName, workspaceText, settings.DiscoveryWaitTimeInSeconds));

            var env            = InitializeEnvironment(sources, settings);
            var outputFilePath = Path.GetTempFileName();
            var arguments      = GetArguments(sources, settings, outputFilePath);

            LogInfo("cd " + settings.WorkingDirectory);
            LogInfo("set " + settings.PathEnv + "=" + env[settings.PathEnv]);
            LogInfo($"{settings.InterpreterPath} {string.Join(" ", arguments)}");

            try {
                var stdout = ProcessExecute.RunWithTimeout(
                    settings.InterpreterPath,
                    env,
                    arguments,
                    settings.WorkingDirectory,
                    settings.PathEnv,
                    settings.DiscoveryWaitTimeInSeconds
                    );
                if (!String.IsNullOrEmpty(stdout))
                {
                    Error(stdout);
                }
            } catch (TimeoutException) {
                Error(Strings.PythonTestDiscovererTimeoutErrorMessage);
                return;
            }

            if (!File.Exists(outputFilePath))
            {
                Error(Strings.PythonDiscoveryResultsNotFound.FormatUI(outputFilePath));
                return;
            }

            string json = File.ReadAllText(outputFilePath);

            if (string.IsNullOrEmpty(json))
            {
                return;
            }

            try {
                var results   = JsonConvert.DeserializeObject <List <PytestDiscoveryResults> >(json);
                var testcases = ParseDiscoveryResults(results, settings.ProjectHome);

                foreach (var tc in testcases)
                {
                    // Note: Test Explorer will show a key not found exception if we use a source path that doesn't match a test container's source.
                    if (settings.TestContainerSources.TryGetValue(tc.CodeFilePath, out _))
                    {
                        discoverySink.SendTestCase(tc);
                    }
                }
            } catch (InvalidOperationException ex) {
                Error("Failed to parse: {0}".FormatInvariant(ex.Message));
                Error(json);
            } catch (JsonException ex) {
                Error("Failed to parse: {0}".FormatInvariant(ex.Message));
                Error(json);
            }
        }
Ejemplo n.º 2
0
        public void DiscoverTests(
            IEnumerable <string> sources,
            IMessageLogger logger,
            ITestCaseDiscoverySink discoverySink
            )
        {
            _logger = logger ?? throw new ArgumentNullException(nameof(logger));
            var workspaceText = _settings.IsWorkspace ? Strings.WorkspaceText : Strings.ProjectText;

            LogInfo(Strings.PythonTestDiscovererStartedMessage.FormatUI(PythonConstants.PytestText, _settings.ProjectName, workspaceText, _settings.DiscoveryWaitTimeInSeconds));

            var env            = InitializeEnvironment(sources, _settings);
            var outputFilePath = Path.GetTempFileName();
            var arguments      = GetArguments(sources, _settings, outputFilePath);

            LogInfo("cd " + _settings.WorkingDirectory);
            LogInfo("set " + _settings.PathEnv + "=" + env[_settings.PathEnv]);
            LogInfo($"{_settings.InterpreterPath} {string.Join(" ", arguments)}");

            try {
                var stdout = ProcessExecute.RunWithTimeout(
                    _settings.InterpreterPath,
                    env,
                    arguments,
                    _settings.WorkingDirectory,
                    _settings.PathEnv,
                    _settings.DiscoveryWaitTimeInSeconds
                    );
                if (!String.IsNullOrEmpty(stdout))
                {
                    Error(stdout);
                }
            } catch (TimeoutException) {
                Error(Strings.PythonTestDiscovererTimeoutErrorMessage);
                return;
            }

            if (!File.Exists(outputFilePath))
            {
                Error(Strings.PythonDiscoveryResultsNotFound.FormatUI(outputFilePath));
                return;
            }

            string json = File.ReadAllText(outputFilePath);

            if (string.IsNullOrEmpty(json))
            {
                return;
            }

            List <PytestDiscoveryResults> results = null;

            try {
                results = JsonConvert.DeserializeObject <List <PytestDiscoveryResults> >(json);
                CreateVsTests(results, discoverySink);
            } catch (InvalidOperationException ex) {
                Error("Failed to parse: {0}".FormatInvariant(ex.Message));
                Error(json);
            } catch (JsonException ex) {
                Error("Failed to parse: {0}".FormatInvariant(ex.Message));
                Error(json);
            }
        }