@@ -435,3 +435,378 @@ For help on these, use 'analyze --help' or 'fix --help'."
435435
436436(module+ main
437437 (resyntax-run))
438+
439+
440+ (module+ test
441+ (require racket/cmdline
442+ racket/file
443+ racket/match
444+ racket/path
445+ racket/port
446+ racket/string
447+ rackunit)
448+
449+
450+ ;; Helper to create temporary test directories and files
451+ (define (make-temp-test-dir)
452+ (make-temporary-file "resyntax-cli-test~a " 'directory ))
453+
454+
455+ (define (make-test-racket-file dir [filename "test.rkt " ] [content "#lang racket/base\n\n(or 1 (or 2 3))\n " ])
456+ (define filepath (build-path dir filename))
457+ (display-to-file content filepath #:exists 'replace )
458+ filepath)
459+
460+
461+ ;; Test helpers for command line parsing
462+ (define (call-with-args proc args)
463+ (parameterize ([current-command-line-arguments (list->vector args)])
464+ (proc)))
465+
466+
467+ (test-case "resyntax-analyze-parse-command-line: parses --file argument "
468+ (define test-dir (make-temp-test-dir))
469+ (define test-file (make-test-racket-file test-dir))
470+
471+ (define options
472+ (call-with-args resyntax-analyze-parse-command-line
473+ (list "--file " (path->string test-file))))
474+
475+ (check-equal? (vector-length (resyntax-analyze-options-targets options)) 1 )
476+ (check-equal? (resyntax-analyze-options-suite options) default-recommendations)
477+ (check-equal? (resyntax-analyze-options-output-format options) plain-text)
478+ (check-equal? (resyntax-analyze-options-output-destination options) 'console )
479+ (check-equal? (resyntax-analyze-options-analyzer-timeout-ms options) 10000 )
480+
481+ (delete-directory/files test-dir))
482+
483+
484+ (test-case "resyntax-analyze-parse-command-line: parses --directory argument "
485+ (define test-dir (make-temp-test-dir))
486+ (make-test-racket-file test-dir)
487+
488+ (define options
489+ (call-with-args resyntax-analyze-parse-command-line
490+ (list "--directory " (path->string test-dir))))
491+
492+ (check-equal? (vector-length (resyntax-analyze-options-targets options)) 1 )
493+
494+ (delete-directory/files test-dir))
495+
496+
497+ (test-case "resyntax-analyze-parse-command-line: parses --analyzer-timeout argument "
498+ (define test-dir (make-temp-test-dir))
499+ (define test-file (make-test-racket-file test-dir))
500+
501+ (define options
502+ (call-with-args resyntax-analyze-parse-command-line
503+ (list "--file " (path->string test-file)
504+ "--analyzer-timeout " "5000 " )))
505+
506+ (check-equal? (resyntax-analyze-options-analyzer-timeout-ms options) 5000 )
507+
508+ (delete-directory/files test-dir))
509+
510+
511+ (test-case "resyntax-analyze-parse-command-line: parses --output-to-file argument "
512+ (define test-dir (make-temp-test-dir))
513+ (define test-file (make-test-racket-file test-dir))
514+ (define output-file (build-path test-dir "output.txt " ))
515+
516+ (define options
517+ (call-with-args resyntax-analyze-parse-command-line
518+ (list "--file " (path->string test-file)
519+ "--output-to-file " (path->string output-file))))
520+
521+ (check-equal? (resyntax-analyze-options-output-destination options)
522+ (simple-form-path output-file))
523+
524+ (delete-directory/files test-dir))
525+
526+
527+ (test-case "resyntax-analyze-parse-command-line: parses --output-as-github-review flag "
528+ (define test-dir (make-temp-test-dir))
529+ (define test-file (make-test-racket-file test-dir))
530+
531+ (define options
532+ (call-with-args resyntax-analyze-parse-command-line
533+ (list "--file " (path->string test-file)
534+ "--output-as-github-review " )))
535+
536+ (check-equal? (resyntax-analyze-options-output-format options) github-pull-request-review)
537+
538+ (delete-directory/files test-dir))
539+
540+
541+ (test-case "resyntax-analyze-parse-command-line: parses multiple --file arguments "
542+ (define test-dir (make-temp-test-dir))
543+ (define test-file1 (make-test-racket-file test-dir "test1.rkt " ))
544+ (define test-file2 (make-test-racket-file test-dir "test2.rkt " ))
545+
546+ (define options
547+ (call-with-args resyntax-analyze-parse-command-line
548+ (list "--file " (path->string test-file1)
549+ "--file " (path->string test-file2))))
550+
551+ (check-equal? (vector-length (resyntax-analyze-options-targets options)) 2 )
552+
553+ (delete-directory/files test-dir))
554+
555+
556+ (test-case "resyntax-fix-parse-command-line: parses --file argument "
557+ (define test-dir (make-temp-test-dir))
558+ (define test-file (make-test-racket-file test-dir))
559+
560+ (define options
561+ (call-with-args resyntax-fix-parse-command-line
562+ (list "--file " (path->string test-file))))
563+
564+ (check-equal? (vector-length (resyntax-fix-options-targets options)) 1 )
565+ (check-equal? (resyntax-fix-options-suite options) default-recommendations)
566+ (check-equal? (resyntax-fix-options-fix-method options) modify-files)
567+ (check-equal? (resyntax-fix-options-output-format options) plain-text)
568+ (check-equal? (resyntax-fix-options-max-fixes options) +inf.0 )
569+ (check-equal? (resyntax-fix-options-max-modified-files options) +inf.0 )
570+ (check-equal? (resyntax-fix-options-max-modified-lines options) +inf.0 )
571+ (check-equal? (resyntax-fix-options-max-pass-count options) 10 )
572+ (check-equal? (resyntax-fix-options-analyzer-timeout-ms options) 10000 )
573+
574+ (delete-directory/files test-dir))
575+
576+
577+ (test-case "resyntax-fix-parse-command-line: parses --directory argument "
578+ (define test-dir (make-temp-test-dir))
579+ (make-test-racket-file test-dir)
580+
581+ (define options
582+ (call-with-args resyntax-fix-parse-command-line
583+ (list "--directory " (path->string test-dir))))
584+
585+ (check-equal? (vector-length (resyntax-fix-options-targets options)) 1 )
586+
587+ (delete-directory/files test-dir))
588+
589+
590+ (test-case "resyntax-fix-parse-command-line: parses --max-fixes argument "
591+ (define test-dir (make-temp-test-dir))
592+ (define test-file (make-test-racket-file test-dir))
593+
594+ (define options
595+ (call-with-args resyntax-fix-parse-command-line
596+ (list "--file " (path->string test-file)
597+ "--max-fixes " "5 " )))
598+
599+ (check-equal? (resyntax-fix-options-max-fixes options) 5 )
600+
601+ (delete-directory/files test-dir))
602+
603+
604+ (test-case "resyntax-fix-parse-command-line: parses --max-modified-files argument "
605+ (define test-dir (make-temp-test-dir))
606+ (define test-file (make-test-racket-file test-dir))
607+
608+ (define options
609+ (call-with-args resyntax-fix-parse-command-line
610+ (list "--file " (path->string test-file)
611+ "--max-modified-files " "3 " )))
612+
613+ (check-equal? (resyntax-fix-options-max-modified-files options) 3 )
614+
615+ (delete-directory/files test-dir))
616+
617+
618+ (test-case "resyntax-fix-parse-command-line: parses --max-modified-lines argument "
619+ (define test-dir (make-temp-test-dir))
620+ (define test-file (make-test-racket-file test-dir))
621+
622+ (define options
623+ (call-with-args resyntax-fix-parse-command-line
624+ (list "--file " (path->string test-file)
625+ "--max-modified-lines " "100 " )))
626+
627+ (check-equal? (resyntax-fix-options-max-modified-lines options) 100 )
628+
629+ (delete-directory/files test-dir))
630+
631+
632+ (test-case "resyntax-fix-parse-command-line: parses --max-pass-count argument "
633+ (define test-dir (make-temp-test-dir))
634+ (define test-file (make-test-racket-file test-dir))
635+
636+ (define options
637+ (call-with-args resyntax-fix-parse-command-line
638+ (list "--file " (path->string test-file)
639+ "--max-pass-count " "5 " )))
640+
641+ (check-equal? (resyntax-fix-options-max-pass-count options) 5 )
642+
643+ (delete-directory/files test-dir))
644+
645+
646+ (test-case "resyntax-fix-parse-command-line: parses --analyzer-timeout argument "
647+ (define test-dir (make-temp-test-dir))
648+ (define test-file (make-test-racket-file test-dir))
649+
650+ (define options
651+ (call-with-args resyntax-fix-parse-command-line
652+ (list "--file " (path->string test-file)
653+ "--analyzer-timeout " "5000 " )))
654+
655+ (check-equal? (resyntax-fix-options-analyzer-timeout-ms options) 5000 )
656+
657+ (delete-directory/files test-dir))
658+
659+
660+ (test-case "resyntax-fix-parse-command-line: parses --output-as-commit-message flag "
661+ (define test-dir (make-temp-test-dir))
662+ (define test-file (make-test-racket-file test-dir))
663+
664+ (define options
665+ (call-with-args resyntax-fix-parse-command-line
666+ (list "--file " (path->string test-file)
667+ "--output-as-commit-message " )))
668+
669+ (check-equal? (resyntax-fix-options-output-format options) git-commit-message)
670+
671+ (delete-directory/files test-dir))
672+
673+
674+ (test-case "resyntax-fix-parse-command-line: parses --output-as-json flag "
675+ (define test-dir (make-temp-test-dir))
676+ (define test-file (make-test-racket-file test-dir))
677+
678+ (define options
679+ (call-with-args resyntax-fix-parse-command-line
680+ (list "--file " (path->string test-file)
681+ "--output-as-json " )))
682+
683+ (check-equal? (resyntax-fix-options-output-format options) json)
684+
685+ (delete-directory/files test-dir))
686+
687+
688+ (test-case "resyntax-fix-parse-command-line: parses --create-multiple-commits flag "
689+ (define test-dir (make-temp-test-dir))
690+ (define test-file (make-test-racket-file test-dir))
691+
692+ (define options
693+ (call-with-args resyntax-fix-parse-command-line
694+ (list "--file " (path->string test-file)
695+ "--create-multiple-commits " )))
696+
697+ (check-equal? (resyntax-fix-options-fix-method options) create-multiple-git-commits)
698+
699+ (delete-directory/files test-dir))
700+
701+
702+ (test-case "resyntax-analyze-run: analyzes a temporary file with refactoring opportunities "
703+ (define test-dir (make-temp-test-dir))
704+ (define test-file (make-test-racket-file test-dir "test.rkt "
705+ "#lang racket/base\n\n(or 1 (or 2 3))\n " ))
706+
707+ (define output
708+ (with-output-to-string
709+ (λ ()
710+ (with-input-from-string ""
711+ (λ ()
712+ (call-with-args resyntax-analyze-run
713+ (list "--file " (path->string test-file))))))))
714+
715+ (check-true (string-contains? output "resyntax: " )
716+ "Output should contain resyntax prefix " )
717+ (check-true (string-contains? output "test.rkt " )
718+ "Output should reference the test file " )
719+
720+ (delete-directory/files test-dir))
721+
722+
723+ (test-case "resyntax-fix-run: fixes a temporary file "
724+ (define test-dir (make-temp-test-dir))
725+ (define test-file (make-test-racket-file test-dir "test.rkt "
726+ "#lang racket/base\n\n(or 1 (or 2 3))\n " ))
727+
728+ ;; Run fix command
729+ (define output
730+ (with-output-to-string
731+ (λ ()
732+ (with-input-from-string ""
733+ (λ ()
734+ (call-with-args resyntax-fix-run
735+ (list "--file " (path->string test-file))))))))
736+
737+ ;; Check that file was modified
738+ (define fixed-content (file->string test-file))
739+ (check-true (string-contains? fixed-content "(or 1 2 3) " )
740+ "File should be fixed to have flattened or expression " )
741+
742+ ;; Check output contains summary
743+ (check-true (string-contains? output "summary " )
744+ "Output should contain summary " )
745+
746+ (delete-directory/files test-dir))
747+
748+
749+ (test-case "resyntax-fix-run: respects --max-fixes limit "
750+ (define test-dir (make-temp-test-dir))
751+ ;; Create a file with multiple issues
752+ (define test-file (make-test-racket-file test-dir "test.rkt "
753+ "#lang racket/base\n\n(or 1 (or 2 3))\n(and 4 (and 5 6))\n " ))
754+
755+ (define output
756+ (with-output-to-string
757+ (λ ()
758+ (with-input-from-string ""
759+ (λ ()
760+ (call-with-args resyntax-fix-run
761+ (list "--file " (path->string test-file)
762+ "--max-fixes " "1 " )))))))
763+
764+ ;; Check that output mentions only 1 fix
765+ (check-true (or (string-contains? output "1 issue " )
766+ (string-contains? output "Fixed 1 " ))
767+ "Output should indicate 1 fix was applied " )
768+
769+ (delete-directory/files test-dir))
770+
771+
772+ (test-case "resyntax-fix-run: --output-as-json produces JSON "
773+ (define test-dir (make-temp-test-dir))
774+ (define test-file (make-test-racket-file test-dir "test.rkt "
775+ "#lang racket/base\n\n(or 1 (or 2 3))\n " ))
776+
777+ (define output
778+ (with-output-to-string
779+ (λ ()
780+ (with-input-from-string ""
781+ (λ ()
782+ (call-with-args resyntax-fix-run
783+ (list "--file " (path->string test-file)
784+ "--output-as-json " )))))))
785+
786+ ;; Check for JSON structure (contains braces and expected keys)
787+ (check-true (string-contains? output "commit_message_body " )
788+ "JSON output should contain commit_message_body key " )
789+ (check-true (string-contains? output "fix_count " )
790+ "JSON output should contain fix_count key " )
791+
792+ (delete-directory/files test-dir))
793+
794+
795+ (test-case "resyntax-fix-run: handles file with no issues "
796+ (define test-dir (make-temp-test-dir))
797+ (define test-file (make-test-racket-file test-dir "test.rkt "
798+ "#lang racket/base\n\n(define x 42)\n " ))
799+
800+ (define output
801+ (with-output-to-string
802+ (λ ()
803+ (with-input-from-string ""
804+ (λ ()
805+ (call-with-args resyntax-fix-run
806+ (list "--file " (path->string test-file))))))))
807+
808+ ;; Check that it indicates no issues found
809+ (check-true (string-contains? output "No issues " )
810+ "Output should indicate no issues found " )
811+
812+ (delete-directory/files test-dir)))
0 commit comments