uncomment 3.0.0

A CLI tool to remove comments from code using tree-sitter for accurate parsing
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
use std::fs;
use std::process::Command;
use tempfile::TempDir;

/// Get the path to the compiled uncomment binary
fn get_binary_path() -> std::path::PathBuf {
    let build_output = Command::new("cargo")
        .args(["build", "--bin", "uncomment"])
        .output()
        .expect("Failed to build binary");

    if !build_output.status.success() {
        panic!(
            "Failed to build binary: {}",
            String::from_utf8_lossy(&build_output.stderr)
        );
    }

    let mut binary_path = std::env::current_dir().expect("Failed to get current directory");
    binary_path.push("target/debug/uncomment");
    binary_path
}

/// Comprehensive integration test for the init command
/// Tests the actual CLI binary to ensure everything works end-to-end
#[test]
fn test_init_command_end_to_end() {
    let temp_dir = TempDir::new().unwrap();
    let project_dir = temp_dir.path();

    create_test_project(project_dir);

    test_smart_init(project_dir);

    test_comprehensive_init(project_dir);

    test_generated_config_processing(project_dir);

    test_force_overwrite(project_dir);
}

fn create_test_project(project_dir: &std::path::Path) {
    fs::create_dir_all(project_dir.join("src")).unwrap();
    fs::create_dir_all(project_dir.join("frontend")).unwrap();
    fs::create_dir_all(project_dir.join("mobile")).unwrap();
    fs::create_dir_all(project_dir.join("scripts")).unwrap();
    fs::create_dir_all(project_dir.join("tests")).unwrap();

    fs::write(
        project_dir.join("src/main.rs"),
        r#"
// Main entry point
fn main() {
    // TODO: implement CLI
    println!("Hello, world!"); // Debug output
    /* Multi-line comment
       with details */
}

/// Documentation for add function
/// Returns the sum of two numbers
fn add(a: i32, b: i32) -> i32 {
    a + b // Simple addition
}
"#,
    )
    .unwrap();

    fs::write(
        project_dir.join("src/lib.rs"),
        r#"
//! Library documentation
//! This is the main library

/// Public API function
pub fn process_data(input: &str) -> String {
    // FIXME: improve error handling
    input.to_uppercase() // Simple transformation
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_process_data() {
        // Test the function
        assert_eq!(process_data("hello"), "HELLO");
    }
}
"#,
    )
    .unwrap();

    fs::write(
        project_dir.join("frontend/app.js"),
        r#"
// Main application
/* eslint-disable no-console */
console.log("Starting app"); // Debug log

/**
 * Main app function
 * @param {string} config - Configuration
 */
function initApp(config) {
    // TODO: add error handling
    console.log("App initialized");
}

// Export for modules
export { initApp };
"#,
    )
    .unwrap();

    fs::write(
        project_dir.join("frontend/types.d.ts"),
        r#"
// Type definitions
/* @ts-ignore missing types */
interface AppConfig {
    // Configuration options
    debug: boolean; // Enable debug mode
    /** API endpoint URL */
    apiUrl: string;
}

// TODO: add more types
export { AppConfig };
"#,
    )
    .unwrap();

    fs::write(
        project_dir.join("frontend/component.tsx"),
        r#"
import React from 'react';

// React component
interface Props {
    // Component props
    title: string; // Display title
}

/**
 * Main component
 * @param props Component properties
 */
const App: React.FC<Props> = ({ title }) => {
    // TODO: add state management
    return (
        <div>
            {/* Main content */}
            <h1>{title}</h1> {/* Title display */}
        </div>
    );
};

export default App;
"#,
    )
    .unwrap();

    fs::write(
        project_dir.join("mobile/ContentView.swift"),
        r#"
import SwiftUI

// Main view
struct ContentView: View {
    // MARK: - Properties
    @State private var counter = 0 // Counter state

    var body: some View {
        VStack {
            // TODO: improve UI design
            Text("Counter: \(counter)") // Display counter
                .padding()

            /* Action button */
            Button("Increment") {
                counter += 1 // Increment action
            }
            .padding() // Add padding
        }
    }
}

/// Preview provider
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
"#,
    )
    .unwrap();

    fs::write(
        project_dir.join("scripts/build.py"),
        r#"
#!/usr/bin/env python3
"""
Build script for the project
"""

import os
import sys

def main():
    """Main build function"""
    # TODO: add proper error handling
    print("Building project...")  # Debug output

    # Check requirements
    if not os.path.exists("Cargo.toml"):  # pragma: no cover
        print("Error: Not a Rust project")  # Error message
        sys.exit(1)

    # Build command
    os.system("cargo build")  # Execute build

if __name__ == "__main__":
    main()  # Run main function
"#,
    )
    .unwrap();

    fs::write(
        project_dir.join("service.go"),
        r#"
package main

import (
    "fmt"
    "log"
)

// Service represents our main service
type Service struct {
    // Configuration
    name string // Service name
    port int    // Port number
}

/*
NewService creates a new service instance
*/
func NewService(name string, port int) *Service {
    // TODO: add validation
    return &Service{
        name: name, // Set name
        port: port, // Set port
    }
}

// Start begins the service
func (s *Service) Start() error {
    // FIXME: implement proper startup
    fmt.Printf("Starting %s on port %d\n", s.name, s.port) // Debug log
    return nil
}

func main() {
    // Create and start service
    svc := NewService("test-service", 8080) // Initialize
    if err := svc.Start(); err != nil {
        log.Fatal(err) // Handle error
    }
}
"#,
    )
    .unwrap();

    fs::write(
        project_dir.join("config.yaml"),
        r#"
# Application configuration
app:
  name: "test-app"  # Application name
  version: "1.0.0"  # Version number

# Database settings
database:
  host: "localhost"  # DB host
  port: 5432         # DB port
  # TODO: add SSL configuration
"#,
    )
    .unwrap();

    fs::write(
        project_dir.join("Dockerfile"),
        r#"
# Multi-stage build
FROM rust:1.70 as builder

# Set working directory
WORKDIR /app

# Copy source
COPY . .

# Build application
# TODO: optimize build layers
RUN cargo build --release

# Runtime stage
FROM debian:bookworm-slim

# Install dependencies
RUN apt-get update && apt-get install -y \
    ca-certificates \
    && rm -rf /var/lib/apt/lists/*

# Copy binary
COPY --from=builder /app/target/release/app /usr/local/bin/app

# Run application
CMD ["app"]
"#,
    )
    .unwrap();

    fs::write(
        project_dir.join("frontend/App.vue"),
        r#"
<template>
  <!-- Main app template -->
  <div id="app">
    <!-- Header section -->
    <header>
      <h1>{{ title }}</h1> <!-- Page title -->
    </header>
    <!-- TODO: add navigation -->
    <main>
      <router-view /> <!-- Router outlet -->
    </main>
  </div>
</template>

<script>
export default {
  name: 'App',
  data() {
    return {
      // Component data
      title: 'My App' // Default title
    }
  },
  // TODO: add lifecycle hooks
  mounted() {
    // Component mounted
    console.log('App mounted') // Debug log
  }
}
</script>

<style>
/* Global styles */
#app {
  font-family: Arial, sans-serif; /* Main font */
  /* TODO: improve styling */
}
</style>
"#,
    )
    .unwrap();

    fs::write(
        project_dir.join("tests/integration_test.py"),
        r#"
"""Integration tests for the application"""

import pytest
import requests

class TestAPI:
    """API integration tests"""

    def test_health_endpoint(self):
        """Test health check endpoint"""
        # TODO: make URL configurable
        response = requests.get("http://localhost:8080/health")  # Health check
        assert response.status_code == 200  # Should be OK

    def test_user_creation(self):
        """Test user creation endpoint"""
        # Test data
        user_data = {
            "name": "Test User",  # User name
            "email": "test@example.com"  # User email
        }

        # Make request
        response = requests.post("http://localhost:8080/users", json=user_data)  # Create user
        # FIXME: add proper assertions
        assert response.status_code in [200, 201]  # Success codes
"#,
    )
    .unwrap();

    fs::write(
        project_dir.join("Makefile"),
        r#"
# Project Makefile

# Default target
.PHONY: all
all: build test

# Build the project
.PHONY: build
build:
	# TODO: parallelize builds
	cargo build --release  # Build Rust code
	cd frontend && npm run build  # Build frontend

# Run tests
.PHONY: test
test:
	# Run all tests
	cargo test  # Rust tests
	cd frontend && npm test  # Frontend tests
	python -m pytest tests/  # Python tests

# Clean build artifacts
.PHONY: clean
clean:
	# Remove build outputs
	cargo clean  # Clean Rust builds
	rm -rf frontend/dist  # Remove frontend dist
"#,
    )
    .unwrap();
}

fn test_smart_init(project_dir: &std::path::Path) {
    println!("Testing smart init...");

    let binary_path = get_binary_path();

    let output = Command::new(&binary_path)
        .args(["init", "--output", "smart-config.toml"])
        .current_dir(project_dir)
        .output()
        .expect("Failed to execute command");

    assert!(
        output.status.success(),
        "Smart init command failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let config_path = project_dir.join("smart-config.toml");
    assert!(config_path.exists(), "Smart config file was not created");

    let config_content = fs::read_to_string(&config_path).unwrap();
    println!("Generated smart config:\n{}", config_content);

    assert!(config_content.contains("[global]"));
    assert!(!config_content.contains("# Smart Uncomment Configuration"));
    assert!(config_content.contains("[languages.rust]"));
    assert!(config_content.contains("[languages.javascript]"));
    assert!(config_content.contains("[languages.typescript]"));
    assert!(config_content.contains("[languages.python]"));
    assert!(config_content.contains("[languages.go]"));
    assert!(config_content.contains("[languages.swift]"));
    assert!(config_content.contains("[languages.vue]"));

    assert!(!config_content.contains("[languages.vue.grammar]"));
    assert!(!config_content.contains("[languages.dockerfile.grammar]"));
    assert!(!config_content.contains("[languages.swift.grammar]"));

    let parsed_config: Result<uncomment::config::Config, _> = toml::from_str(&config_content);
    assert!(
        parsed_config.is_ok(),
        "Generated config should be valid TOML"
    );

    let config = parsed_config.unwrap();
    assert!(
        !config.languages.is_empty(),
        "Should have detected languages"
    );

    println!("✅ Smart init test passed");
}

fn test_comprehensive_init(project_dir: &std::path::Path) {
    println!("Testing comprehensive init...");

    let binary_path = get_binary_path();
    let output = Command::new(&binary_path)
        .args([
            "init",
            "--comprehensive",
            "--output",
            "comprehensive-config.toml",
            "--force",
        ])
        .current_dir(project_dir)
        .output()
        .expect("Failed to execute command");

    assert!(
        output.status.success(),
        "Comprehensive init command failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );

    let config_path = project_dir.join("comprehensive-config.toml");
    assert!(
        config_path.exists(),
        "Comprehensive config file was not created"
    );

    let config_content = fs::read_to_string(&config_path).unwrap();

    assert!(!config_content.contains("# Comprehensive Uncomment Configuration"));

    assert!(config_content.contains("[languages.vue]"));
    assert!(config_content.contains("[languages.svelte]"));
    assert!(config_content.contains("[languages.swift]"));
    assert!(config_content.contains("[languages.kotlin]"));
    assert!(config_content.contains("[languages.dart]"));
    assert!(config_content.contains("[languages.zig]"));
    assert!(config_content.contains("[languages.haskell]"));
    assert!(config_content.contains("[languages.elixir]"));
    assert!(config_content.contains("[languages.julia]"));
    assert!(config_content.contains("[languages.r]"));

    assert!(!config_content.contains("source = { type = \"git\""));

    assert!(!config_content.contains("# Web Development Languages"));
    assert!(!config_content.contains("# Mobile Development"));

    let parsed_config: Result<uncomment::config::Config, _> = toml::from_str(&config_content);
    assert!(
        parsed_config.is_ok(),
        "Comprehensive config should be valid TOML"
    );

    let config = parsed_config.unwrap();
    assert!(
        config.languages.len() >= 10,
        "Should have many languages in comprehensive config, got: {}",
        config.languages.len()
    );

    println!("✅ Comprehensive init test passed");
}

fn test_generated_config_processing(project_dir: &std::path::Path) {
    println!("Testing generated config can process files...");

    let config_path = project_dir.join("smart-config.toml");

    let binary_path = get_binary_path();
    let output = Command::new(&binary_path)
        .args([
            "--config",
            config_path.to_str().unwrap(),
            "--dry-run",
            "--verbose",
            "src/main.rs",
            "frontend/app.js",
            "scripts/build.py",
        ])
        .current_dir(project_dir)
        .output()
        .expect("Failed to execute processing command");

    println!(
        "Processing output: {}",
        String::from_utf8_lossy(&output.stdout)
    );
    if !output.status.success() {
        println!(
            "Processing stderr: {}",
            String::from_utf8_lossy(&output.stderr)
        );
    }

    assert!(
        output.status.success(),
        "File processing with generated config failed"
    );

    let stdout = String::from_utf8_lossy(&output.stdout);

    assert!(
        stdout.contains("files processed") || stdout.contains("comment-free"),
        "Should indicate files were processed: {}",
        stdout
    );

    assert!(
        stdout.contains("files") || stdout.contains("Processing") || stdout.contains("comments"),
        "Should show processing activity"
    );

    println!("✅ Generated config processing test passed");
}

fn test_force_overwrite(project_dir: &std::path::Path) {
    println!("Testing force overwrite...");

    let config_path = project_dir.join("test-force.toml");

    let binary_path = get_binary_path();
    let output1 = Command::new(&binary_path)
        .args(["init", "--output", "test-force.toml"])
        .current_dir(project_dir)
        .output()
        .expect("Failed to execute first init command");

    assert!(output1.status.success(), "First init command failed");
    assert!(config_path.exists(), "Config file should be created");

    let original_content = fs::read_to_string(&config_path).unwrap();

    let output2 = Command::new(&binary_path)
        .args(["init", "--output", "test-force.toml"])
        .current_dir(project_dir)
        .output()
        .expect("Failed to execute second init command");

    assert!(
        !output2.status.success(),
        "Second init without force should fail"
    );

    let stderr = String::from_utf8_lossy(&output2.stderr);
    assert!(
        stderr.contains("already exists") || stderr.contains("Use --force"),
        "Should show error about existing file"
    );

    let output3 = Command::new(&binary_path)
        .args([
            "init",
            "--comprehensive",
            "--output",
            "test-force.toml",
            "--force",
        ])
        .current_dir(project_dir)
        .output()
        .expect("Failed to execute third init command");

    assert!(
        output3.status.success(),
        "Third init with force should succeed"
    );

    let new_content = fs::read_to_string(&config_path).unwrap();
    assert_ne!(
        original_content, new_content,
        "Content should be different after force overwrite"
    );
    assert!(
        new_content.contains("[languages.vue]") && new_content.contains("[languages.kotlin]"),
        "Should have comprehensive config after force overwrite"
    );

    println!("✅ Force overwrite test passed");
}

/// Test error handling scenarios
#[test]
fn test_init_error_scenarios() {
    let temp_dir = TempDir::new().unwrap();
    let project_dir = temp_dir.path();

    let binary_path = get_binary_path();
    let output = Command::new(&binary_path)
        .args(["init", "--output", "/nonexistent/path/config.toml"])
        .current_dir(project_dir)
        .output()
        .expect("Failed to execute command");

    assert!(
        !output.status.success(),
        "Should fail with invalid output path"
    );

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("No such file or directory") || stderr.contains("Failed"),
        "Should show error about invalid path"
    );

    println!("✅ Error scenarios test passed");
}

/// Test help output
#[test]
fn test_init_help() {
    let binary_path = get_binary_path();
    let output = Command::new(&binary_path)
        .args(["init", "--help"])
        .output()
        .expect("Failed to execute help command");

    assert!(output.status.success(), "Help command should succeed");

    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(stdout.contains("Create a template configuration file"));
    assert!(stdout.contains("--comprehensive"));
    assert!(stdout.contains("--interactive"));
    assert!(stdout.contains("--force"));
    assert!(stdout.contains("--output"));

    println!("✅ Help test passed");
}

/// Test that comprehensive config includes expected language repositories
#[test]
fn test_comprehensive_config_repositories() {
    let temp_dir = TempDir::new().unwrap();
    let project_dir = temp_dir.path();

    let binary_path = get_binary_path();
    let output = Command::new(&binary_path)
        .args(["init", "--comprehensive", "--output", "repo-test.toml"])
        .current_dir(project_dir)
        .output()
        .expect("Failed to execute command");

    assert!(output.status.success(), "Comprehensive init should succeed");

    let config_content = fs::read_to_string(project_dir.join("repo-test.toml")).unwrap();

    // Grammar URLs are no longer in templates (tslp handles grammar loading)
    assert!(!config_content.contains("type = \"git\""));

    println!("✅ Repository URLs test passed");
}