コード例 #1
0
ファイル: Program.cs プロジェクト: NYU-OTS/SwaggerCheck
        static void Main(string[] args)
        {
            CommandLineApplication app = new CommandLineApplication();

            var    matchRoute   = false;
            string assemblyPath = "";
            string swaggerPath  = "";

            app.Name = "SwaggerCheck";

            var swaggerRouteOption = app.Option("-s|--swagger",
                                                "Path to Swagger file",
                                                CommandOptionType.SingleValue);

            var assemblyRouteOption = app.Option("-b|--binary",
                                                 "Path to .dll file",
                                                 CommandOptionType.SingleValue);
            var matchRouteOption = app.Option("-r|--routes",
                                              "Find unimplemented Swagger Routes",
                                              CommandOptionType.NoValue);

            app.OnExecute(() =>
            {
                //TODO: change options to arguments https://msdn.microsoft.com/en-us/magazine/mt763239.aspx
                matchRoute = matchRouteOption.HasValue();
                if (swaggerRouteOption.HasValue())
                {
                    swaggerPath = Path.GetFullPath(swaggerRouteOption.Value());
                }
                if (assemblyRouteOption.HasValue())
                {
                    assemblyPath = Path.GetFullPath(assemblyRouteOption.Value());
                }
                return(0);
            });

            app.Execute(args);


            if (!File.Exists(assemblyPath))
            {
                Console.Error.WriteLine($"{assemblyPath} is not a valid path");
                Environment.Exit(1);
            }
            if (!File.Exists(swaggerPath))
            {
                Console.Error.WriteLine($"{assemblyPath} is not a valid path");
                Environment.Exit(1);
            }


            Console.WriteLine($"Program fail on mismatch: {matchRoute}");

            var assembly = new Assembly(assemblyPath);
            var swagger  = new Swagger(swaggerPath);

            Console.WriteLine("Checking Swagger.....");
            var notInAssembly = InSwagger(assembly, swagger);

            Console.WriteLine("Checking Assembly.....");
            var notInSwagger = InAssembly(assembly, swagger);

            Console.WriteLine($"Found {swagger.Endpoints} endpoints in Swagger file");
            Console.WriteLine($"Found {assembly.Endpoints} endpoints in API");
            Console.WriteLine($"{notInAssembly.Count} endpoints have not been implemented");
            foreach (var action in notInAssembly)
            {
                Console.Error.WriteLine($"Warning: No matching endpoint {action.Route} with Http method {action.Method} in API");
            }
            Console.WriteLine($"{notInSwagger.Count} endpoints are implemented but are not in the Swagger file");
            foreach (var action in notInSwagger)
            {
                Console.Error.WriteLine($"Warning: Endpoint {action.Route} with Http method {action.Method} implemented but does not match Swagger file");
            }
            if (matchRoute && (notInAssembly.Any() || notInAssembly.Any()))
            {
                Console.Error.WriteLine("Tests failing");
                Environment.Exit(1);
            }
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: NYU-OTS/SwaggerCheck
 /// <summary>
 /// In Assembly but not in Swagger
 /// </summary>
 /// <param name="assembly">The generated Assembly object</param>
 /// <param name="swagger">The generated Swagger object</param>
 /// <returns></returns>
 static List <Action> InAssembly(Assembly assembly, Swagger swagger)
 {
     return(Compare(assembly.Routes, swagger.Routes));
 }