annotate libpam-sys/libpam-sys-test/build.rs @ 128:ad77f2af5ff4 default tip

Fix `rustc-check-cfg` in `libpam-sys/build.rs`.
author Paul Fisher <paul@pfish.zone>
date Mon, 30 Jun 2025 23:00:53 -0400
parents c77846f3a979
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
110
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
1 use bindgen::MacroTypeVariation;
125
2b255c92417b Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents: 124
diff changeset
2 use libpam_sys_impls::__pam_impl_enum__;
127
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
3 use proc_macro2::{Group, TokenStream, TokenTree};
113
178310336596 Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents: 111
diff changeset
4 use quote::{format_ident, ToTokens};
127
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
5 use std::path::Path;
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
6 use std::process::Command;
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
7 use std::str::FromStr;
110
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
8 use std::{env, fs};
114
93d423b65555 Ignore version-specific OpenPAM constants; generate signed ints.
Paul Fisher <paul@pfish.zone>
parents: 113
diff changeset
9 use syn::{Item, ItemConst, Type, TypePath};
110
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
10
125
2b255c92417b Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents: 124
diff changeset
11 // We're using the macro directly so we can match exhaustively.
2b255c92417b Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents: 124
diff changeset
12 __pam_impl_enum__!();
2b255c92417b Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents: 124
diff changeset
13
110
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
14 fn main() {
121
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
15 let config = match PamImpl::CURRENT {
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
16 PamImpl::LinuxPam => TestConfig {
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
17 headers: vec![
124
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
18 "<security/_pam_types.h>",
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
19 "<security/pam_appl.h>",
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
20 "<security/pam_ext.h>",
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
21 "<security/pam_modules.h>",
121
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
22 ],
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
23 ignore_consts: vec![
124
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
24 "__LINUX_PAM__",
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
25 "__LINUX_PAM_MINOR__",
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
26 "PAM_AUTHTOK_RECOVER_ERR",
121
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
27 ],
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
28 ..Default::default()
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
29 },
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
30 PamImpl::OpenPam => TestConfig {
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
31 headers: vec![
124
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
32 "<security/pam_types.h>",
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
33 "<security/openpam.h>",
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
34 "<security/pam_appl.h>",
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
35 "<security/pam_constants.h>",
121
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
36 ],
124
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
37 ignore_consts: vec!["OPENPAM_VERSION", "OPENPAM_RELEASE", "PAM_SOEXT"],
121
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
38 ..Default::default()
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
39 },
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
40 PamImpl::Sun => TestConfig {
124
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
41 headers: vec!["<security/pam_appl.h>", "<security/pam_modules.h>"],
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
42 block_headers: vec!["sys/.*"],
121
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
43 ..Default::default()
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
44 },
124
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
45 PamImpl::XSso => TestConfig {
125
2b255c92417b Introduce base PAM functions; use the real X/SSO PAM header for tests.
Paul Fisher <paul@pfish.zone>
parents: 124
diff changeset
46 headers: vec!["\"xsso_pam_appl.h\""],
124
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
47 ignore_consts: vec!["PAM_CRED_PRELIM_CHECK"],
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
48 ..Default::default()
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
49 },
121
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
50 };
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
51 generate_const_test(&config);
127
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
52 generate_ctest(&config);
110
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
53 }
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
54
121
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
55 fn generate_const_test(config: &TestConfig) {
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
56 let mut builder = bindgen::Builder::default()
110
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
57 .header_contents("_.h", &config.header_contents())
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
58 .merge_extern_blocks(true)
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
59 .parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
60 .blocklist_type(".*")
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
61 .blocklist_function(".*")
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
62 .allowlist_var(".*")
114
93d423b65555 Ignore version-specific OpenPAM constants; generate signed ints.
Paul Fisher <paul@pfish.zone>
parents: 113
diff changeset
63 .default_macro_constant_type(MacroTypeVariation::Signed);
121
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
64 for hdr in config.block_headers.iter() {
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
65 builder = builder.blocklist_file(".*?/".to_owned() + hdr)
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
66 }
110
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
67
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
68 let generated = builder.generate().unwrap().to_string();
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
69 let file = syn::parse_file(&generated).unwrap();
127
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
70 let mut tests = vec![
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
71 "use libpam_sys::*;".into(),
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
72 "#[allow(deprecated, overflowing_literals)]".into(),
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
73 "fn main() {".into(),
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
74 format!(
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
75 "assert_eq!(PamImpl::CURRENT, PamImpl::{:?});",
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
76 PamImpl::CURRENT
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
77 ),
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
78 ];
110
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
79 tests.extend(
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
80 file.items
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
81 .iter()
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
82 .filter_map(|item| {
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
83 if let Item::Const(item) = item {
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
84 Some(item)
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
85 } else {
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
86 None
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
87 }
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
88 })
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
89 .filter(|item| config.should_check_const(item))
113
178310336596 Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents: 111
diff changeset
90 .cloned()
178310336596 Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents: 111
diff changeset
91 .map(|mut item| {
178310336596 Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents: 111
diff changeset
92 item.ty = Box::new(Type::Path(TypePath {
178310336596 Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents: 111
diff changeset
93 qself: None,
178310336596 Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents: 111
diff changeset
94 path: format_ident!("i32").into(),
178310336596 Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents: 111
diff changeset
95 }));
110
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
96 format!(
127
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
97 "assert_eq!({tokens}, {name});",
113
178310336596 Fix up more constants, make things i32 rather than u32.
Paul Fisher <paul@pfish.zone>
parents: 111
diff changeset
98 tokens = item.expr.to_token_stream(),
110
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
99 name = item.ident
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
100 )
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
101 }),
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
102 );
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
103 tests.push("}".into());
127
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
104 let const_test = test_file("constant_test.rs");
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
105 fs::write(&const_test, tests.join("\n")).unwrap();
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
106 rustfmt(&const_test);
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
107 }
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
108
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
109 fn generate_ctest(config: &TestConfig) {
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
110 let mut test = ctest::TestGenerator::new();
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
111
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
112 for header in config.headers.iter() {
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
113 if header.starts_with('"') {
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
114 test.include(env::var("CARGO_MANIFEST_DIR").unwrap());
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
115 }
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
116 test.header(&header[1..header.len() - 1]);
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
117 }
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
118 // These are opaque structs.
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
119 test.skip_struct(|name| matches!(name, "pam_handle" | "AppData"));
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
120 test.skip_type(|name| matches!(name, "ConversationCallback" | "CleanupCallback"));
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
121 test.type_name(|name, _is_struct, is_union| {
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
122 assert!(!is_union); // we scabbin'
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
123 match name {
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
124 "pam_handle" => "struct pam_handle",
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
125 "pam_conv" => "struct pam_conv",
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
126 "pam_message" => "struct pam_message",
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
127 "pam_response" => "struct pam_response",
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
128 "AppData" => "void",
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
129 other => other,
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
130 }
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
131 .into()
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
132 });
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
133
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
134 //
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
135 // Welcome to THE HACK ZONE.
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
136 //
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
137
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
138 // Define away constness because the various PAM implementations
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
139 // have different const annotations and this will surely drive you crazy.
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
140 test.define("const", Some(""));
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
141
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
142 // Also replace all the `const`s with `mut`s in the ffi.rs file.
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
143 let file_contents = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/../src/ffi.rs"));
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
144 let deconsted_file = test_file("ffi.rs");
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
145 remove_consts(file_contents, &deconsted_file);
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
146
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
147 test.generate(&deconsted_file, "ctest.rs");
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
148 }
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
149
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
150 fn remove_consts(file_contents: &str, out_file: impl AsRef<Path>) {
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
151 let deconstified = deconstify(
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
152 TokenStream::from_str(file_contents).unwrap(),
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
153 &TokenStream::from_str("mut")
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
154 .unwrap()
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
155 .into_iter()
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
156 .next()
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
157 .unwrap(),
110
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
158 )
127
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
159 .to_string();
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
160 let out_file = out_file.as_ref();
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
161 fs::write(out_file, deconstified).unwrap();
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
162 rustfmt(out_file)
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
163 }
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
164
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
165 fn rustfmt(file: impl AsRef<Path>) {
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
166 let status = Command::new(env!("CARGO"))
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
167 .args(["fmt", "--", file.as_ref().to_str().unwrap()])
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
168 .status()
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
169 .unwrap();
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
170 assert!(status.success(), "rustfmt exited with code {status}");
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
171 }
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
172
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
173 fn deconstify(stream: TokenStream, mut_token: &TokenTree) -> TokenStream {
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
174 TokenStream::from_iter(stream.into_iter().map(|token| {
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
175 match token {
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
176 TokenTree::Group(g) => {
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
177 TokenTree::Group(Group::new(g.delimiter(), deconstify(g.stream(), mut_token)))
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
178 .into_token_stream()
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
179 }
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
180 TokenTree::Ident(id) if id == "const" => mut_token.into_token_stream(),
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
181 other => other.into_token_stream(),
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
182 }
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
183 }))
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
184 }
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
185
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
186 fn test_file(name: impl AsRef<str>) -> String {
c77846f3a979 GET CTEST WORKING.
Paul Fisher <paul@pfish.zone>
parents: 125
diff changeset
187 format!("{}/{}", env::var("OUT_DIR").unwrap(), name.as_ref())
110
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
188 }
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
189
121
397743cb70e2 Make libpam-sys-tests work on Illumos!
Paul Fisher <paul@pfish.zone>
parents: 118
diff changeset
190 #[derive(Default)]
110
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
191 struct TestConfig {
124
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
192 headers: Vec<&'static str>,
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
193 block_headers: Vec<&'static str>,
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
194 ignore_consts: Vec<&'static str>,
110
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
195 }
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
196
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
197 impl TestConfig {
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
198 fn header_contents(&self) -> String {
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
199 let vec: Vec<_> = self
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
200 .headers
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
201 .iter()
124
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
202 .map(|h| format!("#include {h}\n"))
110
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
203 .collect();
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
204 vec.join("")
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
205 }
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
206
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
207 fn should_check_const(&self, item: &ItemConst) -> bool {
124
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
208 !self
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
209 .ignore_consts
f469b8d9ad78 Add tests for the original X/SSO constants list.
Paul Fisher <paul@pfish.zone>
parents: 122
diff changeset
210 .contains(&item.ident.to_string().as_ref())
110
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
211 }
2346fd501b7a Add tests for constants and do other macro niceties.
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
212 }