mirror of
https://github.com/neovim/neovim.git
synced 2026-08-29 10:31:48 +00:00
Merge #36859 refactor: clint.py => clint.lua
This commit is contained in:
163
test/functional/fixtures/clint_test.c
Normal file
163
test/functional/fixtures/clint_test.c
Normal file
@@ -0,0 +1,163 @@
|
||||
// Test file to trigger all ERROR_CATEGORIES in clint.lua
|
||||
// This file contains intentional errors to test the linter
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
// build/endif_comment: Uncommented text after #endif
|
||||
#ifdef SOME_CONDITION
|
||||
# define TEST 1
|
||||
#endif SOME_CONDITION
|
||||
|
||||
// build/include_defs: Non-defs header included (but this is a .c file, so might not trigger)
|
||||
|
||||
// build/printf_format: %q format specifier
|
||||
void test_printf_format()
|
||||
{
|
||||
printf("%q", "test"); // Should trigger runtime/printf_format
|
||||
}
|
||||
|
||||
// build/storage_class: Storage class not first
|
||||
const static int x = 5; // Should trigger build/storage_class
|
||||
|
||||
// readability/bool: Use TRUE/FALSE instead of true/false
|
||||
#define TRUE 1
|
||||
#define FALSE 0
|
||||
#define MAYBE 2
|
||||
|
||||
void test_bool()
|
||||
{
|
||||
int flag = TRUE; // Should trigger readability/bool
|
||||
if (flag == FALSE) { // Should trigger readability/bool
|
||||
printf("false\n");
|
||||
}
|
||||
int maybe_val = MAYBE; // Should trigger readability/bool
|
||||
}
|
||||
|
||||
// readability/multiline_comment: Complex multi-line comment
|
||||
void test_multiline_comment()
|
||||
{
|
||||
/* This is a multi-line
|
||||
comment that spans
|
||||
multiple lines and doesn't close properly on the same line */
|
||||
}
|
||||
|
||||
// readability/nul: NUL byte in file (can't easily test this in text)
|
||||
|
||||
// readability/utf8: Invalid UTF-8 (can't easily test)
|
||||
|
||||
// readability/increment: Pre-increment in statements
|
||||
void test_increment()
|
||||
{
|
||||
int i = 0;
|
||||
++i; // Should trigger readability/increment
|
||||
for (int j = 0; j < 10; ++j) { // Should trigger readability/increment
|
||||
printf("%d\n", j);
|
||||
}
|
||||
}
|
||||
|
||||
// runtime/arrays: Variable-length arrays
|
||||
void test_arrays(int size)
|
||||
{
|
||||
int arr[size]; // Should trigger runtime/arrays
|
||||
}
|
||||
|
||||
// runtime/int: Use C basic types instead of fixed-width
|
||||
void test_int_types()
|
||||
{
|
||||
short x = 1; // Should trigger runtime/int
|
||||
long long y = 2; // Should trigger runtime/int
|
||||
}
|
||||
|
||||
// runtime/memset: memset with wrong arguments
|
||||
void test_memset()
|
||||
{
|
||||
char buf[100];
|
||||
memset(buf, sizeof(buf), 0); // Should trigger runtime/memset
|
||||
}
|
||||
|
||||
// runtime/printf: Use sprintf instead of snprintf
|
||||
void test_printf()
|
||||
{
|
||||
char buf[100];
|
||||
sprintf(buf, "test"); // Should trigger runtime/printf
|
||||
}
|
||||
|
||||
// runtime/printf_format: %N$ formats
|
||||
void test_printf_format2()
|
||||
{
|
||||
printf("%1$d", 42); // Should trigger runtime/printf_format
|
||||
}
|
||||
|
||||
// runtime/threadsafe_fn: Use non-thread-safe functions
|
||||
void test_threading()
|
||||
{
|
||||
time_t t;
|
||||
char *time_str = ctime(&t); // Should trigger runtime/threadsafe_fn
|
||||
asctime(localtime(&t)); // Should trigger runtime/threadsafe_fn
|
||||
}
|
||||
|
||||
// runtime/deprecated: (This might be Neovim-specific)
|
||||
|
||||
// whitespace/comments: Missing space after //
|
||||
void test_comments()
|
||||
{
|
||||
int x = 5; // This is a comment // Should trigger whitespace/comments
|
||||
}
|
||||
|
||||
// whitespace/indent: (Hard to test in this format)
|
||||
|
||||
// whitespace/operators: (Hard to test)
|
||||
|
||||
// whitespace/cast: (Hard to test)
|
||||
|
||||
// build/init_macro: INIT() macro in non-header (but this is a .c file)
|
||||
|
||||
// build/header_guard: No #pragma once (but this is a .c file)
|
||||
|
||||
// build/defs_header: extern variables in _defs.h (but this is a .c file)
|
||||
|
||||
// readability/old_style_comment: Old-style /* */ comment
|
||||
void test_old_style_comment()
|
||||
{
|
||||
int x = 5; /* This is an old-style comment */ // Should trigger readability/old_style_comment
|
||||
}
|
||||
|
||||
// Try to trigger more categories
|
||||
void test_more()
|
||||
{
|
||||
// Try strcpy and strncpy
|
||||
char dest[100];
|
||||
char src[] = "test";
|
||||
strcpy(dest, src); // Should trigger runtime/printf
|
||||
strncpy(dest, src, sizeof(dest)); // Should trigger runtime/printf
|
||||
|
||||
// Try malloc and free (should trigger runtime/memory_fn)
|
||||
int *ptr = malloc(sizeof(int)); // Should trigger runtime/memory_fn
|
||||
free(ptr); // Should trigger runtime/memory_fn
|
||||
|
||||
// Try getenv and setenv
|
||||
char *env = getenv("HOME"); // Should trigger runtime/os_fn
|
||||
setenv("TEST", "value", 1); // Should trigger runtime/os_fn
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test_printf_format();
|
||||
test_bool();
|
||||
test_multiline_comment();
|
||||
test_multiline_string();
|
||||
test_increment();
|
||||
test_arrays(10);
|
||||
test_int_types();
|
||||
test_memset();
|
||||
test_printf();
|
||||
test_printf_format2();
|
||||
test_threading();
|
||||
test_comments();
|
||||
test_old_style_comment();
|
||||
test_more();
|
||||
|
||||
return 0;
|
||||
}
|
||||
50
test/functional/script/clint_spec.lua
Normal file
50
test/functional/script/clint_spec.lua
Normal file
@@ -0,0 +1,50 @@
|
||||
local t = require('test.testutil')
|
||||
local n = require('test.functional.testnvim')()
|
||||
|
||||
describe('clint.lua', function()
|
||||
local clint_path = 'src/clint.lua'
|
||||
local test_file = 'test/functional/fixtures/clint_test.c'
|
||||
|
||||
local function run_clint(filepath)
|
||||
local proc = n.spawn_wait('-l', clint_path, filepath)
|
||||
local output = proc:output()
|
||||
local lines = vim.split(output, '\n', { plain = true, trimempty = true })
|
||||
return lines
|
||||
end
|
||||
|
||||
it('a linter lints', function()
|
||||
local output_lines = run_clint(test_file)
|
||||
local expected = {
|
||||
'test/functional/fixtures/clint_test.c:11: Uncommented text after #endif is non-standard. Use a comment. [build/endif_comment] [5]',
|
||||
'test/functional/fixtures/clint_test.c:18: "%q" in format strings is deprecated. Use "%" PRId64 instead. [runtime/printf_format] [3]',
|
||||
'test/functional/fixtures/clint_test.c:22: Storage class (static, extern, typedef, etc) should be first. [build/storage_class] [5]',
|
||||
'test/functional/fixtures/clint_test.c:25: Use true instead of TRUE. [readability/bool] [4]',
|
||||
'test/functional/fixtures/clint_test.c:26: Use false instead of FALSE. [readability/bool] [4]',
|
||||
'test/functional/fixtures/clint_test.c:27: Use kNONE from TriState instead of MAYBE. [readability/bool] [4]',
|
||||
'test/functional/fixtures/clint_test.c:31: Use true instead of TRUE. [readability/bool] [4]',
|
||||
'test/functional/fixtures/clint_test.c:32: Use false instead of FALSE. [readability/bool] [4]',
|
||||
'test/functional/fixtures/clint_test.c:35: Use kNONE from TriState instead of MAYBE. [readability/bool] [4]',
|
||||
'test/functional/fixtures/clint_test.c:41: /*-style comment found, it should be replaced with //-style. /*-style comments are only allowed inside macros. Note that you should not use /*-style comments to document macros itself, use doxygen-style comments for this. [readability/old_style_comment] [5]',
|
||||
'test/functional/fixtures/clint_test.c:54: Do not use preincrement in statements, use postincrement instead [readability/increment] [5]',
|
||||
'test/functional/fixtures/clint_test.c:55: Do not use preincrement in statements, including for(;; action) [readability/increment] [4]',
|
||||
"test/functional/fixtures/clint_test.c:63: Do not use variable-length arrays. Use an appropriately named ('k' followed by CamelCase) compile-time constant for the size. [runtime/arrays] [1]",
|
||||
'test/functional/fixtures/clint_test.c:69: Use int16_t/int64_t/etc, rather than the C type short [runtime/int] [4]',
|
||||
'test/functional/fixtures/clint_test.c:70: Use int16_t/int64_t/etc, rather than the C type long long [runtime/int] [4]',
|
||||
'test/functional/fixtures/clint_test.c:77: Did you mean "memset(buf, 0, sizeof(buf))"? [runtime/memset] [4]',
|
||||
'test/functional/fixtures/clint_test.c:84: Use snprintf instead of sprintf. [runtime/printf] [5]',
|
||||
'test/functional/fixtures/clint_test.c:90: %N$ formats are unconventional. Try rewriting to avoid them. [runtime/printf_format] [2]',
|
||||
'test/functional/fixtures/clint_test.c:97: Use os_ctime_r(...) instead of ctime(...). If it is missing, consider implementing it; see os_localtime_r for an example. [runtime/threadsafe_fn] [2]',
|
||||
'test/functional/fixtures/clint_test.c:98: Use os_asctime_r(...) instead of asctime(...). If it is missing, consider implementing it; see os_localtime_r for an example. [runtime/threadsafe_fn] [2]',
|
||||
'test/functional/fixtures/clint_test.c:98: Use os_localtime_r(...) instead of localtime(...). If it is missing, consider implementing it; see os_localtime_r for an example. [runtime/threadsafe_fn] [2]',
|
||||
'test/functional/fixtures/clint_test.c:124: /*-style comment found, it should be replaced with //-style. /*-style comments are only allowed inside macros. Note that you should not use /*-style comments to document macros itself, use doxygen-style comments for this. [readability/old_style_comment] [5]',
|
||||
'test/functional/fixtures/clint_test.c:133: Use xstrlcpy, xmemcpyz or snprintf instead of strcpy [runtime/printf] [4]',
|
||||
'test/functional/fixtures/clint_test.c:134: Use xstrlcpy, xmemcpyz or snprintf instead of strncpy (unless this is from Vim) [runtime/printf] [4]',
|
||||
'test/functional/fixtures/clint_test.c:137: Use xmalloc(...) instead of malloc(...). [runtime/memory_fn] [2]',
|
||||
'test/functional/fixtures/clint_test.c:138: Use xfree(...) instead of free(...). [runtime/memory_fn] [2]',
|
||||
'test/functional/fixtures/clint_test.c:141: Use os_getenv(...) instead of getenv(...). [runtime/os_fn] [2]',
|
||||
'test/functional/fixtures/clint_test.c:142: Use os_setenv(...) instead of setenv(...). [runtime/os_fn] [2]',
|
||||
'Total errors found: 28',
|
||||
}
|
||||
t.eq(expected, output_lines)
|
||||
end)
|
||||
end)
|
||||
Reference in New Issue
Block a user